Re: [PATCH v2 5/5] iio: pressure: bmp280: Add nvmem operations for BMP580

Angel Iglesias
2023-01-01T11:48:11Z

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