Message ID | 92664164d24cbd9c6541cdbd73b163dbf964ab68.1672062380.git.ang.iglesiasg@gmail.com |
---|---|
State | New |
Headers | show |
Series | Add support for pressure sensor Bosch BMP580 | expand |
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; > +} > +
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
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); + 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); + 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); + 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_preinit(struct bmp280_data *data) { + struct nvmem_config config = { + .dev = data->dev, + .priv = data, + .name = "bmp580_nvmem", + .word_size = sizeof(u16), + .stride = sizeof(u16), + .size = 3 * sizeof(u16), + .reg_read = bmp580_nvmem_read, + .reg_write = bmp580_nvmem_write, + }; unsigned int reg; int ret; @@ -1653,8 +1786,8 @@ static int bmp580_preinit(struct bmp280_data *data) dev_err(data->dev, "preinit: nvm error on powerup sequence\n"); return -EIO; } - - return 0; + /* Register nvmem device */ + return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config)); } static int bmp580_chip_config(struct bmp280_data *data) diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 27d2abc17d01..e2a093a4f767 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -101,6 +101,9 @@ #define BMP580_FILTER_63X 6 #define BMP580_FILTER_127X 7 +#define BMP580_NVM_ROW_ADDR_MASK GENMASK(5, 0) +#define BMP580_NVM_PROG_EN BIT(6) + #define BMP580_TEMP_SKIPPED 0x7f7f7f #define BMP580_PRESS_SKIPPED 0x7f7f7f
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>