linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Olivier Moysan <olivier.moysan@foss.st.com>
To: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: Olivier Moysan <olivier.moysan@foss.st.com>,
	<linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: [RFC v2 08/11] iio: adc: sd modulator: add scale and offset support
Date: Thu, 27 Jul 2023 17:03:19 +0200	[thread overview]
Message-ID: <20230727150324.1157933-9-olivier.moysan@foss.st.com> (raw)
In-Reply-To: <20230727150324.1157933-1-olivier.moysan@foss.st.com>

Register the SD modulator as an IIO backend device instead of
a standard IIO device. Expose scale and offset information to
IIO backend consumer.

Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
 drivers/iio/adc/sd_adc_modulator.c | 106 +++++++++++++++++++++++------
 1 file changed, 86 insertions(+), 20 deletions(-)

diff --git a/drivers/iio/adc/sd_adc_modulator.c b/drivers/iio/adc/sd_adc_modulator.c
index 327cc2097f6c..e77e7884c403 100644
--- a/drivers/iio/adc/sd_adc_modulator.c
+++ b/drivers/iio/adc/sd_adc_modulator.c
@@ -6,44 +6,110 @@
  * 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/regulator/consumer.h>
 
-static const struct iio_info iio_sd_mod_iio_info;
+struct iio_sd_mod_priv {
+	struct regulator *vref;
+	int vref_mv;
+};
 
-static const struct iio_chan_spec iio_sd_mod_ch = {
-	.type = IIO_VOLTAGE,
-	.indexed = 1,
-	.scan_type = {
-		.sign = 'u',
-		.realbits = 1,
-		.shift = 0,
-	},
+static int sd_mod_enable(struct iio_backend *backend)
+{
+	struct iio_sd_mod_priv *priv = backend->priv;
+	int ret;
+
+	ret = regulator_enable(priv->vref);
+	if (ret) {
+		dev_err(&backend->dev, "Failed to enable regulator: %d\n", ret);
+		return ret;
+	}
+
+	ret = regulator_get_voltage(priv->vref);
+	priv->vref_mv = ret / 1000;
+
+	return 0;
+};
+
+static int sd_mod_disable(struct iio_backend *backend)
+{
+	struct iio_sd_mod_priv *priv = backend->priv;
+
+	return regulator_disable(priv->vref);
+};
+
+static int sd_mod_read(struct iio_backend *backend, int *val, int *val2, long mask)
+{
+	struct iio_sd_mod_priv *priv = backend->priv;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		*val = priv->vref_mv;
+		*val2 = 0;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_OFFSET:
+		*val = 0;
+		*val2 = 0;
+		return IIO_VAL_INT;
+	}
+
+	return -EINVAL;
+};
+
+static const struct iio_backend_ops sd_mod_ops = {
+	.enable = sd_mod_enable,
+	.disable = sd_mod_disable,
+	.read_raw = sd_mod_read,
 };
 
 static int iio_sd_mod_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct iio_dev *iio;
+	struct regulator *vref;
+	struct iio_backend *backend;
+	struct iio_sd_mod_priv *priv;
+	int ret;
 
-	iio = devm_iio_device_alloc(dev, 0);
-	if (!iio)
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
 		return -ENOMEM;
 
-	iio->name = dev_name(dev);
-	iio->info = &iio_sd_mod_iio_info;
-	iio->modes = INDIO_BUFFER_HARDWARE;
+	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 {
+		priv->vref = vref;
+	}
 
-	iio->num_channels = 1;
-	iio->channels = &iio_sd_mod_ch;
+	backend = iio_backend_alloc(dev);
+	if (!backend) {
+		dev_err(dev, "Failed to allocate sd modulator backend data\n");
+		return -ENOMEM;
+	}
+
+	backend->priv = priv;
+	backend->ops = &sd_mod_ops;
+
+	ret = iio_backend_register(backend);
+	if (ret) {
+		dev_err(dev, "Failed to register backend: %d\n", ret);
+		goto err_free_backend;
+	}
 
-	platform_set_drvdata(pdev, iio);
+	return 0;
 
-	return devm_iio_device_register(&pdev->dev, iio);
-}
+err_free_backend:
+	iio_backend_free(&backend->dev);
+
+	return ret;
+};
 
 static const struct of_device_id sd_adc_of_match[] = {
 	{ .compatible = "sd-modulator" },
-- 
2.25.1


      parent reply	other threads:[~2023-07-27 15:06 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27 15:03 [RFC v2 00/11] iio: add iio backend device type Olivier Moysan
2023-07-27 15:03 ` [RFC v2 01/11] iio: introduce iio backend device Olivier Moysan
2023-07-28  8:42   ` Nuno Sá
2023-08-31 16:14     ` Olivier MOYSAN
2023-09-01  8:01       ` Nuno Sá
2023-09-03 10:46         ` Jonathan Cameron
2023-09-05 10:06         ` Olivier MOYSAN
2023-09-11  9:39           ` Nuno Sá
2023-09-18 15:52             ` Olivier MOYSAN
2023-09-22  8:53               ` Nuno Sá
2023-09-25  6:48                 ` Nuno Sá
2023-09-26 16:44                   ` Olivier MOYSAN
2023-09-28  7:15                     ` Nuno Sá
2023-09-28 16:30                       ` Olivier MOYSAN
2023-07-27 15:03 ` [RFC v2 03/11] dt-bindings: iio: stm32-dfsdm-adc: add scaling support Olivier Moysan
2023-08-11 17:10   ` Rob Herring
2023-08-31 15:53     ` Olivier MOYSAN
2023-07-27 15:03 ` [RFC v2 04/11] dt-bindings: iio: adc: add scaling support to sd modulator Olivier Moysan
2023-07-27 15:03 ` [RFC v2 05/11] iio: adc: stm32-dfsdm: manage dfsdm as a channel provider Olivier Moysan
2023-07-27 15:03 ` [RFC v2 06/11] iio: adc: stm32-dfsdm: adopt generic channel bindings Olivier Moysan
2023-07-27 15:03 ` [RFC v2 07/11] iio: adc: stm32-dfsdm: add scaling support to dfsdm Olivier Moysan
2023-07-27 15:03 ` Olivier Moysan [this message]

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=20230727150324.1157933-9-olivier.moysan@foss.st.com \
    --to=olivier.moysan@foss.st.com \
    --cc=broonie@kernel.org \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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).