1 /*
2 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/module.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/adc/ad_sigma_delta.h>
24
25 #define AD7780_RDY BIT(7)
26 #define AD7780_FILTER BIT(6)
27 #define AD7780_ERR BIT(5)
28 #define AD7780_ID1 BIT(4)
29 #define AD7780_ID0 BIT(3)
30 #define AD7780_GAIN BIT(2)
31 #define AD7780_PAT1 BIT(1)
32 #define AD7780_PAT0 BIT(0)
33
34 struct ad7780_chip_info {
35 struct iio_chan_spec channel;
36 unsigned int pattern_mask;
37 unsigned int pattern;
38 };
39
40 struct ad7780_state {
41 const struct ad7780_chip_info *chip_info;
42 struct regulator *reg;
43 struct gpio_desc *powerdown_gpio;
44 unsigned int gain;
45 u16 int_vref_mv;
46
47 struct ad_sigma_delta sd;
48 };
49
50 enum ad7780_supported_device_ids {
51 ID_AD7170,
52 ID_AD7171,
53 ID_AD7780,
54 ID_AD7781,
55 };
56
ad_sigma_delta_to_ad7780(struct ad_sigma_delta * sd)57 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
58 {
59 return container_of(sd, struct ad7780_state, sd);
60 }
61
ad7780_set_mode(struct ad_sigma_delta * sigma_delta,enum ad_sigma_delta_mode mode)62 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
63 enum ad_sigma_delta_mode mode)
64 {
65 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
66 unsigned int val;
67
68 switch (mode) {
69 case AD_SD_MODE_SINGLE:
70 case AD_SD_MODE_CONTINUOUS:
71 val = 1;
72 break;
73 default:
74 val = 0;
75 break;
76 }
77
78 gpiod_set_value(st->powerdown_gpio, val);
79
80 return 0;
81 }
82
ad7780_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)83 static int ad7780_read_raw(struct iio_dev *indio_dev,
84 struct iio_chan_spec const *chan,
85 int *val,
86 int *val2,
87 long m)
88 {
89 struct ad7780_state *st = iio_priv(indio_dev);
90 int voltage_uv;
91
92 switch (m) {
93 case IIO_CHAN_INFO_RAW:
94 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
95 case IIO_CHAN_INFO_SCALE:
96 voltage_uv = regulator_get_voltage(st->reg);
97 if (voltage_uv < 0)
98 return voltage_uv;
99 *val = (voltage_uv / 1000) * st->gain;
100 *val2 = chan->scan_type.realbits - 1;
101 return IIO_VAL_FRACTIONAL_LOG2;
102 case IIO_CHAN_INFO_OFFSET:
103 *val -= (1 << (chan->scan_type.realbits - 1));
104 return IIO_VAL_INT;
105 }
106
107 return -EINVAL;
108 }
109
ad7780_postprocess_sample(struct ad_sigma_delta * sigma_delta,unsigned int raw_sample)110 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
111 unsigned int raw_sample)
112 {
113 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
114 const struct ad7780_chip_info *chip_info = st->chip_info;
115
116 if ((raw_sample & AD7780_ERR) ||
117 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
118 return -EIO;
119
120 if (raw_sample & AD7780_GAIN)
121 st->gain = 1;
122 else
123 st->gain = 128;
124
125 return 0;
126 }
127
128 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
129 .set_mode = ad7780_set_mode,
130 .postprocess_sample = ad7780_postprocess_sample,
131 .has_registers = false,
132 };
133
134 #define AD7780_CHANNEL(bits, wordsize) \
135 AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
136
137 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
138 [ID_AD7170] = {
139 .channel = AD7780_CHANNEL(12, 24),
140 .pattern = 0x5,
141 .pattern_mask = 0x7,
142 },
143 [ID_AD7171] = {
144 .channel = AD7780_CHANNEL(16, 24),
145 .pattern = 0x5,
146 .pattern_mask = 0x7,
147 },
148 [ID_AD7780] = {
149 .channel = AD7780_CHANNEL(24, 32),
150 .pattern = 0x1,
151 .pattern_mask = 0x3,
152 },
153 [ID_AD7781] = {
154 .channel = AD7780_CHANNEL(20, 32),
155 .pattern = 0x1,
156 .pattern_mask = 0x3,
157 },
158 };
159
160 static const struct iio_info ad7780_info = {
161 .read_raw = ad7780_read_raw,
162 .driver_module = THIS_MODULE,
163 };
164
ad7780_probe(struct spi_device * spi)165 static int ad7780_probe(struct spi_device *spi)
166 {
167 struct ad7780_state *st;
168 struct iio_dev *indio_dev;
169 int ret, voltage_uv = 0;
170
171 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
172 if (!indio_dev)
173 return -ENOMEM;
174
175 st = iio_priv(indio_dev);
176 st->gain = 1;
177
178 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
179
180 st->reg = devm_regulator_get(&spi->dev, "avdd");
181 if (IS_ERR(st->reg))
182 return PTR_ERR(st->reg);
183
184 ret = regulator_enable(st->reg);
185 if (ret) {
186 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
187 return ret;
188 }
189 voltage_uv = regulator_get_voltage(st->reg);
190
191 st->chip_info =
192 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
193
194 if (voltage_uv)
195 st->int_vref_mv = voltage_uv / 1000;
196 else
197 dev_warn(&spi->dev, "Reference voltage unspecified\n");
198
199 spi_set_drvdata(spi, indio_dev);
200
201 indio_dev->dev.parent = &spi->dev;
202 indio_dev->name = spi_get_device_id(spi)->name;
203 indio_dev->modes = INDIO_DIRECT_MODE;
204 indio_dev->channels = &st->chip_info->channel;
205 indio_dev->num_channels = 1;
206 indio_dev->info = &ad7780_info;
207
208 st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
209 "powerdown",
210 GPIOD_OUT_LOW);
211 if (IS_ERR(st->powerdown_gpio)) {
212 ret = PTR_ERR(st->powerdown_gpio);
213 dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
214 ret);
215 goto error_disable_reg;
216 }
217
218 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
219 if (ret)
220 goto error_disable_reg;
221
222 ret = iio_device_register(indio_dev);
223 if (ret)
224 goto error_cleanup_buffer_and_trigger;
225
226 return 0;
227
228 error_cleanup_buffer_and_trigger:
229 ad_sd_cleanup_buffer_and_trigger(indio_dev);
230 error_disable_reg:
231 regulator_disable(st->reg);
232
233 return ret;
234 }
235
ad7780_remove(struct spi_device * spi)236 static int ad7780_remove(struct spi_device *spi)
237 {
238 struct iio_dev *indio_dev = spi_get_drvdata(spi);
239 struct ad7780_state *st = iio_priv(indio_dev);
240
241 iio_device_unregister(indio_dev);
242 ad_sd_cleanup_buffer_and_trigger(indio_dev);
243
244 regulator_disable(st->reg);
245
246 return 0;
247 }
248
249 static const struct spi_device_id ad7780_id[] = {
250 {"ad7170", ID_AD7170},
251 {"ad7171", ID_AD7171},
252 {"ad7780", ID_AD7780},
253 {"ad7781", ID_AD7781},
254 {}
255 };
256 MODULE_DEVICE_TABLE(spi, ad7780_id);
257
258 static struct spi_driver ad7780_driver = {
259 .driver = {
260 .name = "ad7780",
261 },
262 .probe = ad7780_probe,
263 .remove = ad7780_remove,
264 .id_table = ad7780_id,
265 };
266 module_spi_driver(ad7780_driver);
267
268 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
269 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
270 MODULE_LICENSE("GPL v2");
271