Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Olivier Moysan <olivier.moysan@foss.st.com>
Cc: <fabrice.gasnier@foss.st.com>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>, <linux-iio@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v6 8/9] iio: add iio backend support to sd modulator
Date: Tue, 6 Aug 2024 17:51:06 +0100	[thread overview]
Message-ID: <20240806175106.09a53784@jic23-huawei> (raw)
In-Reply-To: <20240730084640.1307938-9-olivier.moysan@foss.st.com>

On Tue, 30 Jul 2024 10:46:38 +0200
Olivier Moysan <olivier.moysan@foss.st.com> wrote:

> The legacy sd modulator driver registers the sigma delta modulator as
> an IIO channel provider. This implementation is not convenient when the
> SD modulator has to be cascaded with another IIO device. The scaling
> information is distributed across devices, which makes it difficult to
> report consistent scaling data on IIO devices.
> 
> The solution is to expose these cascaded IIO devices as an aggregate
> device, which report global scaling information.
> Add IIO backend support to SD modulator to allow scaling information
> management.
> 
> Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
I've applied this fixup given changes in the backend code that crossed
with this.

diff --git a/drivers/iio/adc/sd_adc_modulator.c b/drivers/iio/adc/sd_adc_modulator.c
index 06f9c5cacd53..654b6a38b650 100644
--- a/drivers/iio/adc/sd_adc_modulator.c
+++ b/drivers/iio/adc/sd_adc_modulator.c
@@ -74,6 +74,11 @@ static const struct iio_backend_ops sd_backend_ops = {
        .read_raw = iio_sd_mod_read,
 };
 
+static const struct iio_backend_info sd_backend_info = {
+       .name = "sd-modulator",
+       .ops = &sd_backend_ops,
+};
+
 static int iio_sd_mod_register(struct platform_device *pdev)
 {
        struct device *dev = &pdev->dev;
@@ -131,7 +136,7 @@ static int iio_sd_mod_probe(struct platform_device *pdev)
                priv->vref_mv = ret / 1000;
        }
 
-       return devm_iio_backend_register(&pdev->dev, &sd_backend_ops, priv);
+       return devm_iio_backend_register(&pdev->dev, &sd_backend_info, priv);
 };

Please give it a quick spin. Hopefully I didn't break anything.

Jonathan

> ---
>  drivers/iio/adc/Kconfig            |  1 +
>  drivers/iio/adc/sd_adc_modulator.c | 92 +++++++++++++++++++++++++++++-
>  2 files changed, 92 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index bd028d59db63..43ff8182b2ea 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -1195,6 +1195,7 @@ config SD_ADC_MODULATOR
>  	tristate "Generic sigma delta modulator"
>  	select IIO_BUFFER
>  	select IIO_TRIGGERED_BUFFER
> +	select IIO_BACKEND
>  	help
>  	  Select this option to enables sigma delta modulator. This driver can
>  	  support generic sigma delta modulators.
> diff --git a/drivers/iio/adc/sd_adc_modulator.c b/drivers/iio/adc/sd_adc_modulator.c
> index 327cc2097f6c..06f9c5cacd53 100644
> --- a/drivers/iio/adc/sd_adc_modulator.c
> +++ b/drivers/iio/adc/sd_adc_modulator.c
> @@ -6,11 +6,14 @@
>   * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>.
>   */
>  
> +#include <linux/iio/backend.h>
>  #include <linux/iio/iio.h>
>  #include <linux/iio/triggered_buffer.h>
>  #include <linux/module.h>
>  #include <linux/mod_devicetable.h>
>  #include <linux/platform_device.h>
> +#include <linux/property.h>
> +#include <linux/regulator/consumer.h>
>  
>  static const struct iio_info iio_sd_mod_iio_info;
>  
> @@ -24,7 +27,54 @@ static const struct iio_chan_spec iio_sd_mod_ch = {
>  	},
>  };
>  
> -static int iio_sd_mod_probe(struct platform_device *pdev)
> +struct iio_sd_backend_priv {
> +	struct regulator *vref;
> +	int vref_mv;
> +};
> +
> +static int iio_sd_mod_enable(struct iio_backend *backend)
> +{
> +	struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend);
> +
> +	if (priv->vref)diff --git a/drivers/iio/adc/sd_adc_modulator.c b/drivers/iio/adc/sd_adc_modulator.c
index 06f9c5cacd53..654b6a38b650 100644
--- a/drivers/iio/adc/sd_adc_modulator.c
+++ b/drivers/iio/adc/sd_adc_modulator.c
@@ -74,6 +74,11 @@ static const struct iio_backend_ops sd_backend_ops = {
        .read_raw = iio_sd_mod_read,
 };
 
+static const struct iio_backend_info sd_backend_info = {
+       .name = "sd-modulator",
+       .ops = &sd_backend_ops,
+};
+
 static int iio_sd_mod_register(struct platform_device *pdev)
 {
        struct device *dev = &pdev->dev;
@@ -131,7 +136,7 @@ static int iio_sd_mod_probe(struct platform_device *pdev)
                priv->vref_mv = ret / 1000;
        }
 
-       return devm_iio_backend_register(&pdev->dev, &sd_backend_ops, priv);
+       return devm_iio_backend_register(&pdev->dev, &sd_backend_info, priv);
 };
> +		return regulator_enable(priv->vref);
> +
> +	return 0;
> +};
> +
> +static void iio_sd_mod_disable(struct iio_backend *backend)
> +{
> +	struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend);
> +
> +	if (priv->vref)
> +		regulator_disable(priv->vref);
> +};
> +
> +static int iio_sd_mod_read(struct iio_backend *backend, struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SCALE:
> +		*val = priv->vref_mv;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = 0;
> +		return IIO_VAL_INT;
> +	}
> +
> +	return -EOPNOTSUPP;
> +};
> +
> +static const struct iio_backend_ops sd_backend_ops = {
> +	.enable = iio_sd_mod_enable,
> +	.disable = iio_sd_mod_disable,
> +	.read_raw = iio_sd_mod_read,
> +};
> +
> +static int iio_sd_mod_register(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct iio_dev *iio;
> @@ -45,6 +95,45 @@ static int iio_sd_mod_probe(struct platform_device *pdev)
>  	return devm_iio_device_register(&pdev->dev, iio);
>  }
>  
> +static int iio_sd_mod_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct regulator *vref;
> +	struct iio_sd_backend_priv *priv;
> +	int ret;
> +
> +	/* If sd modulator is not defined as an IIO backend device, fallback to legacy */
> +	if (!device_property_present(dev, "#io-backend-cells"))
> +		return iio_sd_mod_register(pdev);
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	/*
> +	 * Get regulator reference if any, but don't enable regulator right now.
> +	 * Rely on enable and disable callbacks to manage regulator power.
> +	 */
> +	vref = devm_regulator_get_optional(dev, "vref");
> +	if (IS_ERR(vref)) {
> +		if (PTR_ERR(vref) != -ENODEV)
> +			return dev_err_probe(dev, PTR_ERR(vref), "Failed to get vref\n");
> +	} else {
> +		/*
> +		 * Retrieve voltage right now, as regulator_get_voltage() provides it whatever
> +		 * the state of the regulator.
> +		 */
> +		ret = regulator_get_voltage(vref);
> +		if (ret < 0)
> +			return ret;
> +
> +		priv->vref = vref;
> +		priv->vref_mv = ret / 1000;
> +	}
> +
> +	return devm_iio_backend_register(&pdev->dev, &sd_backend_ops, priv);
> +};
> +
>  static const struct of_device_id sd_adc_of_match[] = {
>  	{ .compatible = "sd-modulator" },
>  	{ .compatible = "ads1201" },
> @@ -65,3 +154,4 @@ module_platform_driver(iio_sd_mod_adc);
>  MODULE_DESCRIPTION("Basic sigma delta modulator");
>  MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
>  MODULE_LICENSE("GPL v2");
> +MODULE_IMPORT_NS(IIO_BACKEND);


  reply	other threads:[~2024-08-06 16:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30  8:46 [PATCH v6 0/9] iio: adc: dfsdm: add scaling support Olivier Moysan
2024-07-30  8:46 ` [PATCH v6 1/9] iio: add read scale and offset services to iio backend framework Olivier Moysan
2024-07-30  8:46 ` [PATCH v6 2/9] iio: add enable and disable " Olivier Moysan
2024-07-30  8:46 ` [PATCH v6 3/9] iio: add child nodes support in " Olivier Moysan
2024-07-30  8:46 ` [PATCH v6 4/9] dt-bindings: iio: dfsdm: move to " Olivier Moysan
2024-07-30  8:46 ` [PATCH v6 5/9] dt-bindings: iio: add backend support to sd modulator Olivier Moysan
2024-08-06 14:49   ` Rob Herring (Arm)
2024-07-30  8:46 ` [PATCH v6 6/9] dt-bindings: iio: add vref " Olivier Moysan
2024-08-06 14:49   ` Rob Herring (Arm)
2024-07-30  8:46 ` [PATCH v6 7/9] iio: adc: stm32-dfsdm: adopt generic channels bindings Olivier Moysan
2024-08-03 15:12   ` Jonathan Cameron
2024-07-30  8:46 ` [PATCH v6 8/9] iio: add iio backend support to sd modulator Olivier Moysan
2024-08-06 16:51   ` Jonathan Cameron [this message]
2024-08-07  7:40     ` Olivier MOYSAN
2024-07-30  8:46 ` [PATCH v6 9/9] iio: adc: stm32-dfsdm: add scaling support to dfsdm Olivier Moysan
2024-08-03 15:13 ` [PATCH v6 0/9] iio: adc: dfsdm: add scaling support Jonathan Cameron
2024-08-06 16:39   ` Jonathan Cameron

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=20240806175106.09a53784@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=broonie@kernel.org \
    --cc=fabrice.gasnier@foss.st.com \
    --cc=lars@metafoo.de \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=olivier.moysan@foss.st.com \
    /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