lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20221230184928.011a7851@jic23-huawei>
Date:   Fri, 30 Dec 2022 18:49:28 +0000
From:   Jonathan Cameron <jic23@...nel.org>
To:     Angel Iglesias <ang.iglesiasg@...il.com>
Cc:     linux-iio@...r.kernel.org, Lars-Peter Clausen <lars@...afoo.de>,
        Rob Herring <robh+dt@...nel.org>,
        Krzysztof Kozlowski <krzysztof.kozlowski+dt@...aro.org>,
        Nikita Yushchenko <nikita.yoush@...entembedded.com>,
        Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        Ulf Hansson <ulf.hansson@...aro.org>,
        "Rafael J. Wysocki" <rafael.j.wysocki@...el.com>,
        Paul Cercueil <paul@...pouillou.net>,
        Andreas Klinger <ak@...klinger.de>, devicetree@...r.kernel.org,
        linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 5/5] iio: pressure: bmp280: Add nvmem operations for
 BMP580

On Mon, 26 Dec 2022 15:29:24 +0100
Angel Iglesias <ang.iglesiasg@...il.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@...il.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;
> +}
> +


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ