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