linux/drivers/iio/adc/sd_adc_modulator.c

// SPDX-License-Identifier: GPL-2.0
/*
 * Generic sigma delta modulator driver
 *
 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
 * Author: Arnaud Pouliquen <[email protected]>.
 */

#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;

static const struct iio_chan_spec iio_sd_mod_ch =;

struct iio_sd_backend_priv {};

static int iio_sd_mod_enable(struct iio_backend *backend)
{
	struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend);

	if (priv->vref)
		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 =;

static const struct iio_backend_info sd_backend_info =;

static int iio_sd_mod_register(struct platform_device *pdev)
{}

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_info, priv);
};

static const struct of_device_id sd_adc_of_match[] =;
MODULE_DEVICE_TABLE(of, sd_adc_of_match);

static struct platform_driver iio_sd_mod_adc =;

module_platform_driver();

MODULE_DESCRIPTION();
MODULE_AUTHOR();
MODULE_LICENSE();
MODULE_IMPORT_NS();