summaryrefslogtreecommitdiff
path: root/drivers/iio/adc/sd_adc_modulator.c
blob: 654b6a38b650a197c28bf1341c4ca6a7ea0cc82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: GPL-2.0
/*
 * Generic sigma delta modulator driver
 *
 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
 * 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;

static const struct iio_chan_spec iio_sd_mod_ch = {
	.type = IIO_VOLTAGE,
	.indexed = 1,
	.scan_type = {
		.sign = 'u',
		.realbits = 1,
		.shift = 0,
	},
};

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)
		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 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;
	struct iio_dev *iio;

	iio = devm_iio_device_alloc(dev, 0);
	if (!iio)
		return -ENOMEM;

	iio->name = dev_name(dev);
	iio->info = &iio_sd_mod_iio_info;
	iio->modes = INDIO_BUFFER_HARDWARE;

	iio->num_channels = 1;
	iio->channels = &iio_sd_mod_ch;

	platform_set_drvdata(pdev, iio);

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

static const struct of_device_id sd_adc_of_match[] = {
	{ .compatible = "sd-modulator" },
	{ .compatible = "ads1201" },
	{ }
};
MODULE_DEVICE_TABLE(of, sd_adc_of_match);

static struct platform_driver iio_sd_mod_adc = {
	.driver = {
		.name = "iio_sd_adc_mod",
		.of_match_table = sd_adc_of_match,
	},
	.probe = iio_sd_mod_probe,
};

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