Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Alex Lanzano <lanzano.alex@gmail.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	linux-kernel-mentees@lists.linuxfoundation.org,
	skhan@linuxfoundation.org, linux-kernel@vger.kernel.org,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH] iio: imu: bmi270: Add spi driver for bmi270 imu
Date: Sun, 29 Sep 2024 15:54:37 +0100	[thread overview]
Message-ID: <20240929155437.60115014@jic23-huawei> (raw)
In-Reply-To: <20240927183717.3613601-1-lanzano.alex@gmail.com>

On Fri, 27 Sep 2024 14:37:10 -0400
Alex Lanzano <lanzano.alex@gmail.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@gmail.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.

> +}


  reply	other threads:[~2024-09-29 14:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-27 18:37 [PATCH] iio: imu: bmi270: Add spi driver for bmi270 imu Alex Lanzano
2024-09-29 14:54 ` Jonathan Cameron [this message]
2024-09-29 19:31   ` Alex Lanzano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240929155437.60115014@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=lanzano.alex@gmail.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox