forked from torvalds/linux
/
sd_adc_modulator.c
130 lines (108 loc) · 2.87 KB
/
sd_adc_modulator.c
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
// 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/iio.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/regulator/consumer.h>
static const struct iio_chan_spec iio_sd_mod_ch = {
.type = IIO_VOLTAGE,
.indexed = 1,
.scan_type = {
.sign = 'u',
.realbits = 1,
.shift = 0,
},
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
};
static const struct iio_chan_spec iio_sd_mod_ch_ads = {
.type = IIO_VOLTAGE,
.indexed = 1,
.scan_type = {
.sign = 'u',
.realbits = 1,
.shift = 0,
},
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
.differential = 1,
};
struct iio_sd_mod_priv {
int vref_mv;
};
static const struct of_device_id sd_adc_of_match[] = {
{ .compatible = "sd-modulator", .data = &iio_sd_mod_ch },
{ .compatible = "ads1201", .data = &iio_sd_mod_ch_ads },
{ }
};
static int iio_sd_mod_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
{
struct iio_sd_mod_priv *priv = iio_priv(indio_dev);
switch (mask) {
case IIO_CHAN_INFO_SCALE:
*val = priv->vref_mv;
*val2 = chan->scan_type.realbits;
return IIO_VAL_INT;
}
return -EINVAL;
}
static const struct iio_info iio_sd_mod_iio_info = {
.read_raw = iio_sd_mod_read_raw,
};
static int iio_sd_mod_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct iio_sd_mod_priv *priv;
struct regulator *vref;
struct iio_dev *iio;
int ret;
iio = devm_iio_device_alloc(dev, sizeof(*priv));
if (!iio)
return -ENOMEM;
iio->channels = (const struct iio_chan_spec *)
of_match_device(sd_adc_of_match, &pdev->dev)->data;
priv = iio_priv(iio);
iio->dev.parent = dev;
iio->dev.of_node = dev->of_node;
iio->name = dev_name(dev);
iio->info = &iio_sd_mod_iio_info;
iio->modes = INDIO_BUFFER_HARDWARE;
iio->num_channels = 1;
vref = devm_regulator_get_optional(dev, "vref");
if (IS_ERR(vref)) {
ret = PTR_ERR(vref);
if (ret != -ENODEV) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "vref get failed, %d\n", ret);
return ret;
}
}
if (!IS_ERR(vref)) {
ret = regulator_get_voltage(vref);
if (ret < 0) {
dev_err(dev, "vref get failed, %d\n", ret);
return ret;
}
priv->vref_mv = ret / 1000;
dev_dbg(dev, "vref+=%dmV\n", priv->vref_mv);
}
return devm_iio_device_register(&pdev->dev, iio);
}
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");