devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Angel Iglesias <ang.iglesiasg@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: linux-iio@vger.kernel.org, Lars-Peter Clausen <lars@metafoo.de>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Nikita Yushchenko <nikita.yoush@cogentembedded.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	Paul Cercueil <paul@crapouillou.net>,
	Andreas Klinger <ak@it-klinger.de>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 5/5] iio: pressure: bmp280: Add nvmem operations for BMP580
Date: Sun, 01 Jan 2023 12:48:02 +0100	[thread overview]
Message-ID: <4e7c7e14b6eb99cc4007b9d6f8528c49e532cd26.camel@gmail.com> (raw)
In-Reply-To: <20221230184928.011a7851@jic23-huawei>

On Fri, 2022-12-30 at 18:49 +0000, Jonathan Cameron wrote:
> On Mon, 26 Dec 2022 15:29:24 +0100
> Angel Iglesias <ang.iglesiasg@gmail.com> wrote:
> 
> > The pressure sensor BMP580 contains a non-volatile memory that stores
> > trimming and configuration params. That memory provides an programmable
> > user range of three 2-byte words.
> > 
> > Signed-off-by: Angel Iglesias <ang.iglesiasg@gmail.com>
> 
> Not much in this one from me other than follow on from earlier patch.
> Thanks,
> 
> Jonathan
> 
> > 
> > diff --git a/drivers/iio/pressure/bmp280-core.c
> > b/drivers/iio/pressure/bmp280-core.c
> > index 44901c6eb2f9..578d145be55d 100644
> > --- a/drivers/iio/pressure/bmp280-core.c
> > +++ b/drivers/iio/pressure/bmp280-core.c
> > @@ -28,6 +28,7 @@
> >  #include <linux/bitfield.h>
> >  #include <linux/device.h>
> >  #include <linux/module.h>
> > +#include <linux/nvmem-provider.h>
> >  #include <linux/regmap.h>
> >  #include <linux/delay.h>
> >  #include <linux/iio/iio.h>
> > @@ -1628,8 +1629,140 @@ static const int bmp580_odr_table[][2] = {
> >         [BMP580_ODR_0_125HZ] =  {0, 125000},
> >  };
> >  
> > +const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
> > +
> > +static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
> > +                            size_t bytes)
> > +{
> > +       struct bmp280_data *data = priv;
> > +       u16 *dst = val;
> > +       int ret, addr;
> > +
> > +       pm_runtime_get_sync(data->dev);
> > +       mutex_lock(&data->lock);
> > +
> > +       /* Set sensor in standby mode */
> > +       ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
> > +                                BMP580_MODE_MASK |
> > BMP580_ODR_DEEPSLEEP_DIS,
> > +                                BMP580_ODR_DEEPSLEEP_DIS |
> > +                                FIELD_PREP(BMP580_MODE_MASK,
> > BMP580_MODE_SLEEP));
> > +       if (ret) {
> > +               dev_err(data->dev, "failed to change sensor to standby
> > mode\n");
> > +               goto exit;
> > +       }
> > +       /* Wait standby transition time */
> > +       usleep_range(2500, 3000);
> > +
> > +       while (bytes >= sizeof(u16)) {
> > +               addr = bmp580_nvmem_addrs[offset / sizeof(u16)];
> > +
> > +               ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
> > +                                  FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK,
> > addr));
> > +               if (ret) {
> > +                       dev_err(data->dev, "error writing nvm address\n");
> > +                       goto exit;
> > +               }
> > +
> > +               ret = bmp580_cmd(data, BMP580_NVM_READ_CMD);
> Ah. Here is the command being used.  Good to pull that code forwards to this
> patch.
> 
> > +               if (ret)
> > +                       goto exit;
> > +
> > +               ret = regmap_bulk_read(data->regmap,
> > BMP580_REG_NVM_DATA_LSB, &data->le16,
> > +                                      sizeof(data->le16));
> > +               if (ret) {
> > +                       dev_err(data->dev, "error reading nvm data regs\n");
> > +                       goto exit;
> > +               }
> > +
> > +               *dst++ = le16_to_cpu(data->le16);
> > +               bytes -= sizeof(u16);
> 
> sizeof(le16) seems more appropriate (obviously it's the same value).
> 
> > +               offset += sizeof(u16);
> > +       }
> > +exit:
> > +       /* Restore chip config */
> > +       data->chip_info->chip_config(data);
> > +       mutex_unlock(&data->lock);
> > +       pm_runtime_mark_last_busy(data->dev);
> > +       pm_runtime_put_autosuspend(data->dev);
> > +       return ret;
> > +}
> > +
> > +static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
> > +                             size_t bytes)
> > +{
> > +       struct bmp280_data *data = priv;
> > +       u16 *buf = val;
> > +       int ret, addr;
> > +
> > +       pm_runtime_get_sync(data->dev);
> > +       mutex_lock(&data->lock);
> > +
> > +       /* Set sensor in standby mode */
> > +       ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
> > +                                BMP580_MODE_MASK |
> > BMP580_ODR_DEEPSLEEP_DIS,
> > +                                BMP580_ODR_DEEPSLEEP_DIS |
> > +                                FIELD_PREP(BMP580_MODE_MASK,
> > BMP580_MODE_SLEEP));
> > +       if (ret) {
> > +               dev_err(data->dev, "failed to change sensor to standby
> > mode\n");
> > +               goto exit;
> > +       }
> > +       /* Wait standby transition time */
> > +       usleep_range(2500, 3000);
> > +
> > +       while (bytes >= sizeof(u16)) {
> > +               addr = bmp580_nvmem_addrs[offset / sizeof(u16)];
> > +
> > +               ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
> > BMP580_NVM_PROG_EN |
> > +                                  FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK,
> > addr));
> > +               if (ret) {
> > +                       dev_err(data->dev, "error writing nvm address\n");
> > +                       goto exit;
> > +               }
> > +               data->le16 = cpu_to_le16(*buf++);
> > +
> > +               ret = regmap_bulk_write(data->regmap,
> > BMP580_REG_NVM_DATA_LSB, &data->le16,
> > +                                       sizeof(data->le16));
> > +               if (ret) {
> > +                       dev_err(data->dev, "error writing LSB NVM data
> > regs\n");
> > +                       goto exit;
> > +               }
> > +
> > +               ret = bmp580_cmd(data, BMP580_NVM_WRITE_CMD);
> > +               if (ret)
> > +                       goto exit;
> > +
> > +               /* Disable programming mode bit */
> > +               ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
> > +                                        BMP580_NVM_PROG_EN, 0);
> > +               if (ret) {
> > +                       dev_err(data->dev, "error resetting nvm write\n");
> > +                       goto exit;
> > +               }
> > +
> > +               bytes -= sizeof(u16);
> 
> As above, maybe sizeof(le16)
> 
> > +               offset += sizeof(u16);
> > +       }
> > +exit:
> > +       /* Restore chip config */
> > +       data->chip_info->chip_config(data);
> > +       mutex_unlock(&data->lock);
> > +       pm_runtime_mark_last_busy(data->dev);
> > +       pm_runtime_put_autosuspend(data->dev);
> > +       return ret;
> > +}
> > +
> 
> 
Got it,
Thanks for your time,
Angel



  reply	other threads:[~2023-01-01 11:48 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-26 14:29 [PATCH v2 0/5] Add support for pressure sensor Bosch BMP580 Angel Iglesias
2022-12-26 14:29 ` [PATCH v2 1/5] iio: pressure: bmp280: Add enumeration to handle chip variants Angel Iglesias
2022-12-27 21:37   ` Andy Shevchenko
2023-01-01 11:04     ` Angel Iglesias
2023-01-08 12:41       ` Jonathan Cameron
2022-12-30 18:14   ` Jonathan Cameron
2023-01-01 10:56     ` Angel Iglesias
2022-12-26 14:29 ` [PATCH v2 2/5] iio: pressure: bmp280: Add preinit callback Angel Iglesias
2022-12-27 21:41   ` Andy Shevchenko
2023-01-01 11:06     ` Angel Iglesias
2022-12-30 18:18   ` Jonathan Cameron
2023-01-01 11:09     ` Angel Iglesias
2022-12-26 14:29 ` [PATCH v2 3/5] iio: pressure: bmp280: Add support for new sensor BMP580 Angel Iglesias
2022-12-29 17:35   ` Christophe JAILLET
2022-12-29 18:23     ` Angel Iglesias
2022-12-30 18:22       ` Jonathan Cameron
2023-01-01 11:16         ` Angel Iglesias
2023-01-08 12:35           ` Jonathan Cameron
2023-01-12 10:38             ` Contact Bosch-Sensortec (BST/SA)
2022-12-30 18:45   ` Jonathan Cameron
2023-01-01 11:46     ` Angel Iglesias
2023-01-08 12:38       ` Jonathan Cameron
2022-12-26 14:29 ` [PATCH v2 4/5] dt-bindings: iio: pressure: bmp085: Add BMP580 compatible string Angel Iglesias
2022-12-27  8:11   ` Krzysztof Kozlowski
2022-12-26 14:29 ` [PATCH v2 5/5] iio: pressure: bmp280: Add nvmem operations for BMP580 Angel Iglesias
2022-12-30 18:49   ` Jonathan Cameron
2023-01-01 11:48     ` Angel Iglesias [this message]
2022-12-26 22:05 ` [PATCH v2 4/5] dt-bindings: iio: pressure: bmp085: Add BMP580 compatible string Rob Herring

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=4e7c7e14b6eb99cc4007b9d6f8528c49e532cd26.camel@gmail.com \
    --to=ang.iglesiasg@gmail.com \
    --cc=ak@it-klinger.de \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nikita.yoush@cogentembedded.com \
    --cc=paul@crapouillou.net \
    --cc=rafael.j.wysocki@intel.com \
    --cc=robh+dt@kernel.org \
    --cc=ulf.hansson@linaro.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;
as well as URLs for NNTP newsgroup(s).