aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2012-11-21 11:27:00 -0500
committerJonathan Cameron <jic23@kernel.org>2012-11-30 07:57:08 -0500
commitf87f1a2375a51ef8c5048bfce42587dbea1ca627 (patch)
tree8738bc7e299e45272933477510ad208d957a0795 /drivers/iio
parente786cc26dcc52caba53d17a80888ed0b46d097f8 (diff)
staging:iio: Move ad7793 driver out of staging
The driver does not expose any custom API to userspace and none of the standard static code checker tools report any issues, so move it out of staging. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/adc/Kconfig12
-rw-r--r--drivers/iio/adc/Makefile1
-rw-r--r--drivers/iio/adc/ad7793.c691
3 files changed, 704 insertions, 0 deletions
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index cd5eed60be28..408557b02441 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -42,6 +42,18 @@ config AD7791
42 To compile this driver as a module, choose M here: the module will be 42 To compile this driver as a module, choose M here: the module will be
43 called ad7791. 43 called ad7791.
44 44
45config AD7793
46 tristate "Analog Devices AD7793 and similar ADCs driver"
47 depends on SPI
48 select AD_SIGMA_DELTA
49 help
50 Say yes here to build support for Analog Devices AD7785, AD7792, AD7793,
51 AD7794 and AD7795 SPI analog to digital converters (ADC).
52 If unsure, say N (but it's safe to say "Y").
53
54 To compile this driver as a module, choose M here: the
55 module will be called AD7793.
56
45config AD7476 57config AD7476
46 tristate "Analog Devices AD7476 and similar 1-channel ADCs driver" 58 tristate "Analog Devices AD7476 and similar 1-channel ADCs driver"
47 depends on SPI 59 depends on SPI
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 3256dc64a466..78202d9eb961 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_AD7266) += ad7266.o
7obj-$(CONFIG_AD7298) += ad7298.o 7obj-$(CONFIG_AD7298) += ad7298.o
8obj-$(CONFIG_AD7476) += ad7476.o 8obj-$(CONFIG_AD7476) += ad7476.o
9obj-$(CONFIG_AD7791) += ad7791.o 9obj-$(CONFIG_AD7791) += ad7791.o
10obj-$(CONFIG_AD7793) += ad7793.o
10obj-$(CONFIG_AD7887) += ad7887.o 11obj-$(CONFIG_AD7887) += ad7887.o
11obj-$(CONFIG_AT91_ADC) += at91_adc.o 12obj-$(CONFIG_AT91_ADC) += at91_adc.o
12obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 13obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
new file mode 100644
index 000000000000..a109e686b9f7
--- /dev/null
+++ b/drivers/iio/adc/ad7793.c
@@ -0,0 +1,691 @@
1/*
2 * AD7785/AD7792/AD7793/AD7794/AD7795 SPI ADC driver
3 *
4 * Copyright 2011-2012 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/delay.h>
19#include <linux/module.h>
20
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/buffer.h>
24#include <linux/iio/trigger.h>
25#include <linux/iio/trigger_consumer.h>
26#include <linux/iio/triggered_buffer.h>
27#include <linux/iio/adc/ad_sigma_delta.h>
28#include <linux/platform_data/ad7793.h>
29
30/* Registers */
31#define AD7793_REG_COMM 0 /* Communications Register (WO, 8-bit) */
32#define AD7793_REG_STAT 0 /* Status Register (RO, 8-bit) */
33#define AD7793_REG_MODE 1 /* Mode Register (RW, 16-bit */
34#define AD7793_REG_CONF 2 /* Configuration Register (RW, 16-bit) */
35#define AD7793_REG_DATA 3 /* Data Register (RO, 16-/24-bit) */
36#define AD7793_REG_ID 4 /* ID Register (RO, 8-bit) */
37#define AD7793_REG_IO 5 /* IO Register (RO, 8-bit) */
38#define AD7793_REG_OFFSET 6 /* Offset Register (RW, 16-bit
39 * (AD7792)/24-bit (AD7793)) */
40#define AD7793_REG_FULLSALE 7 /* Full-Scale Register
41 * (RW, 16-bit (AD7792)/24-bit (AD7793)) */
42
43/* Communications Register Bit Designations (AD7793_REG_COMM) */
44#define AD7793_COMM_WEN (1 << 7) /* Write Enable */
45#define AD7793_COMM_WRITE (0 << 6) /* Write Operation */
46#define AD7793_COMM_READ (1 << 6) /* Read Operation */
47#define AD7793_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
48#define AD7793_COMM_CREAD (1 << 2) /* Continuous Read of Data Register */
49
50/* Status Register Bit Designations (AD7793_REG_STAT) */
51#define AD7793_STAT_RDY (1 << 7) /* Ready */
52#define AD7793_STAT_ERR (1 << 6) /* Error (Overrange, Underrange) */
53#define AD7793_STAT_CH3 (1 << 2) /* Channel 3 */
54#define AD7793_STAT_CH2 (1 << 1) /* Channel 2 */
55#define AD7793_STAT_CH1 (1 << 0) /* Channel 1 */
56
57/* Mode Register Bit Designations (AD7793_REG_MODE) */
58#define AD7793_MODE_SEL(x) (((x) & 0x7) << 13) /* Operation Mode Select */
59#define AD7793_MODE_SEL_MASK (0x7 << 13) /* Operation Mode Select mask */
60#define AD7793_MODE_CLKSRC(x) (((x) & 0x3) << 6) /* ADC Clock Source Select */
61#define AD7793_MODE_RATE(x) ((x) & 0xF) /* Filter Update Rate Select */
62
63#define AD7793_MODE_CONT 0 /* Continuous Conversion Mode */
64#define AD7793_MODE_SINGLE 1 /* Single Conversion Mode */
65#define AD7793_MODE_IDLE 2 /* Idle Mode */
66#define AD7793_MODE_PWRDN 3 /* Power-Down Mode */
67#define AD7793_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
68#define AD7793_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
69#define AD7793_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
70#define AD7793_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
71
72#define AD7793_CLK_INT 0 /* Internal 64 kHz Clock not
73 * available at the CLK pin */
74#define AD7793_CLK_INT_CO 1 /* Internal 64 kHz Clock available
75 * at the CLK pin */
76#define AD7793_CLK_EXT 2 /* External 64 kHz Clock */
77#define AD7793_CLK_EXT_DIV2 3 /* External Clock divided by 2 */
78
79/* Configuration Register Bit Designations (AD7793_REG_CONF) */
80#define AD7793_CONF_VBIAS(x) (((x) & 0x3) << 14) /* Bias Voltage
81 * Generator Enable */
82#define AD7793_CONF_BO_EN (1 << 13) /* Burnout Current Enable */
83#define AD7793_CONF_UNIPOLAR (1 << 12) /* Unipolar/Bipolar Enable */
84#define AD7793_CONF_BOOST (1 << 11) /* Boost Enable */
85#define AD7793_CONF_GAIN(x) (((x) & 0x7) << 8) /* Gain Select */
86#define AD7793_CONF_REFSEL(x) ((x) << 6) /* INT/EXT Reference Select */
87#define AD7793_CONF_BUF (1 << 4) /* Buffered Mode Enable */
88#define AD7793_CONF_CHAN(x) ((x) & 0xf) /* Channel select */
89#define AD7793_CONF_CHAN_MASK 0xf /* Channel select mask */
90
91#define AD7793_CH_AIN1P_AIN1M 0 /* AIN1(+) - AIN1(-) */
92#define AD7793_CH_AIN2P_AIN2M 1 /* AIN2(+) - AIN2(-) */
93#define AD7793_CH_AIN3P_AIN3M 2 /* AIN3(+) - AIN3(-) */
94#define AD7793_CH_AIN1M_AIN1M 3 /* AIN1(-) - AIN1(-) */
95#define AD7793_CH_TEMP 6 /* Temp Sensor */
96#define AD7793_CH_AVDD_MONITOR 7 /* AVDD Monitor */
97
98#define AD7795_CH_AIN4P_AIN4M 4 /* AIN4(+) - AIN4(-) */
99#define AD7795_CH_AIN5P_AIN5M 5 /* AIN5(+) - AIN5(-) */
100#define AD7795_CH_AIN6P_AIN6M 6 /* AIN6(+) - AIN6(-) */
101#define AD7795_CH_AIN1M_AIN1M 8 /* AIN1(-) - AIN1(-) */
102
103/* ID Register Bit Designations (AD7793_REG_ID) */
104#define AD7785_ID 0xB
105#define AD7792_ID 0xA
106#define AD7793_ID 0xB
107#define AD7794_ID 0xF
108#define AD7795_ID 0xF
109#define AD7793_ID_MASK 0xF
110
111/* IO (Excitation Current Sources) Register Bit Designations (AD7793_REG_IO) */
112#define AD7793_IO_IEXC1_IOUT1_IEXC2_IOUT2 0 /* IEXC1 connect to IOUT1,
113 * IEXC2 connect to IOUT2 */
114#define AD7793_IO_IEXC1_IOUT2_IEXC2_IOUT1 1 /* IEXC1 connect to IOUT2,
115 * IEXC2 connect to IOUT1 */
116#define AD7793_IO_IEXC1_IEXC2_IOUT1 2 /* Both current sources
117 * IEXC1,2 connect to IOUT1 */
118#define AD7793_IO_IEXC1_IEXC2_IOUT2 3 /* Both current sources
119 * IEXC1,2 connect to IOUT2 */
120
121#define AD7793_IO_IXCEN_10uA (1 << 0) /* Excitation Current 10uA */
122#define AD7793_IO_IXCEN_210uA (2 << 0) /* Excitation Current 210uA */
123#define AD7793_IO_IXCEN_1mA (3 << 0) /* Excitation Current 1mA */
124
125/* NOTE:
126 * The AD7792/AD7793 features a dual use data out ready DOUT/RDY output.
127 * In order to avoid contentions on the SPI bus, it's therefore necessary
128 * to use spi bus locking.
129 *
130 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
131 */
132
133struct ad7793_chip_info {
134 unsigned int id;
135 const struct iio_chan_spec *channels;
136 unsigned int num_channels;
137};
138
139struct ad7793_state {
140 const struct ad7793_chip_info *chip_info;
141 struct regulator *reg;
142 u16 int_vref_mv;
143 u16 mode;
144 u16 conf;
145 u32 scale_avail[8][2];
146
147 struct ad_sigma_delta sd;
148
149};
150
151enum ad7793_supported_device_ids {
152 ID_AD7785,
153 ID_AD7792,
154 ID_AD7793,
155 ID_AD7794,
156 ID_AD7795,
157};
158
159static struct ad7793_state *ad_sigma_delta_to_ad7793(struct ad_sigma_delta *sd)
160{
161 return container_of(sd, struct ad7793_state, sd);
162}
163
164static int ad7793_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
165{
166 struct ad7793_state *st = ad_sigma_delta_to_ad7793(sd);
167
168 st->conf &= ~AD7793_CONF_CHAN_MASK;
169 st->conf |= AD7793_CONF_CHAN(channel);
170
171 return ad_sd_write_reg(&st->sd, AD7793_REG_CONF, 2, st->conf);
172}
173
174static int ad7793_set_mode(struct ad_sigma_delta *sd,
175 enum ad_sigma_delta_mode mode)
176{
177 struct ad7793_state *st = ad_sigma_delta_to_ad7793(sd);
178
179 st->mode &= ~AD7793_MODE_SEL_MASK;
180 st->mode |= AD7793_MODE_SEL(mode);
181
182 return ad_sd_write_reg(&st->sd, AD7793_REG_MODE, 2, st->mode);
183}
184
185static const struct ad_sigma_delta_info ad7793_sigma_delta_info = {
186 .set_channel = ad7793_set_channel,
187 .set_mode = ad7793_set_mode,
188 .has_registers = true,
189 .addr_shift = 3,
190 .read_mask = BIT(6),
191};
192
193static const struct ad_sd_calib_data ad7793_calib_arr[6] = {
194 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN1P_AIN1M},
195 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN1P_AIN1M},
196 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN2P_AIN2M},
197 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN2P_AIN2M},
198 {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN3P_AIN3M},
199 {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN3P_AIN3M}
200};
201
202static int ad7793_calibrate_all(struct ad7793_state *st)
203{
204 return ad_sd_calibrate_all(&st->sd, ad7793_calib_arr,
205 ARRAY_SIZE(ad7793_calib_arr));
206}
207
208static int ad7793_setup(struct iio_dev *indio_dev,
209 const struct ad7793_platform_data *pdata,
210 unsigned int vref_mv)
211{
212 struct ad7793_state *st = iio_priv(indio_dev);
213 int i, ret = -1;
214 unsigned long long scale_uv;
215 u32 id;
216
217 if ((pdata->current_source_direction == AD7793_IEXEC1_IEXEC2_IOUT1 ||
218 pdata->current_source_direction == AD7793_IEXEC1_IEXEC2_IOUT2) &&
219 ((pdata->exitation_current != AD7793_IX_10uA) &&
220 (pdata->exitation_current != AD7793_IX_210uA)))
221 return -EINVAL;
222
223 /* reset the serial interface */
224 ret = spi_write(st->sd.spi, (u8 *)&ret, sizeof(ret));
225 if (ret < 0)
226 goto out;
227 usleep_range(500, 2000); /* Wait for at least 500us */
228
229 /* write/read test for device presence */
230 ret = ad_sd_read_reg(&st->sd, AD7793_REG_ID, 1, &id);
231 if (ret)
232 goto out;
233
234 id &= AD7793_ID_MASK;
235
236 if (id != st->chip_info->id) {
237 dev_err(&st->sd.spi->dev, "device ID query failed\n");
238 goto out;
239 }
240
241 st->mode = AD7793_MODE_RATE(1);
242 st->mode |= AD7793_MODE_CLKSRC(pdata->clock_src);
243 st->conf = AD7793_CONF_REFSEL(pdata->refsel);
244 st->conf |= AD7793_CONF_VBIAS(pdata->bias_voltage);
245 if (pdata->buffered)
246 st->conf |= AD7793_CONF_BUF;
247 if (pdata->boost_enable)
248 st->conf |= AD7793_CONF_BOOST;
249 if (pdata->burnout_current)
250 st->conf |= AD7793_CONF_BO_EN;
251 if (pdata->unipolar)
252 st->conf |= AD7793_CONF_UNIPOLAR;
253
254 ret = ad7793_set_mode(&st->sd, AD_SD_MODE_IDLE);
255 if (ret)
256 goto out;
257
258 ret = ad7793_set_channel(&st->sd, 0);
259 if (ret)
260 goto out;
261
262 ret = ad_sd_write_reg(&st->sd, AD7793_REG_IO, 1,
263 pdata->exitation_current |
264 (pdata->current_source_direction << 2));
265 if (ret)
266 goto out;
267
268 ret = ad7793_calibrate_all(st);
269 if (ret)
270 goto out;
271
272 /* Populate available ADC input ranges */
273 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
274 scale_uv = ((u64)vref_mv * 100000000)
275 >> (st->chip_info->channels[0].scan_type.realbits -
276 (!!(st->conf & AD7793_CONF_UNIPOLAR) ? 0 : 1));
277 scale_uv >>= i;
278
279 st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
280 st->scale_avail[i][0] = scale_uv;
281 }
282
283 return 0;
284out:
285 dev_err(&st->sd.spi->dev, "setup failed\n");
286 return ret;
287}
288
289static const u16 sample_freq_avail[16] = {0, 470, 242, 123, 62, 50, 39, 33, 19,
290 17, 16, 12, 10, 8, 6, 4};
291
292static ssize_t ad7793_read_frequency(struct device *dev,
293 struct device_attribute *attr,
294 char *buf)
295{
296 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
297 struct ad7793_state *st = iio_priv(indio_dev);
298
299 return sprintf(buf, "%d\n",
300 sample_freq_avail[AD7793_MODE_RATE(st->mode)]);
301}
302
303static ssize_t ad7793_write_frequency(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf,
306 size_t len)
307{
308 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
309 struct ad7793_state *st = iio_priv(indio_dev);
310 long lval;
311 int i, ret;
312
313 mutex_lock(&indio_dev->mlock);
314 if (iio_buffer_enabled(indio_dev)) {
315 mutex_unlock(&indio_dev->mlock);
316 return -EBUSY;
317 }
318 mutex_unlock(&indio_dev->mlock);
319
320 ret = kstrtol(buf, 10, &lval);
321 if (ret)
322 return ret;
323
324 ret = -EINVAL;
325
326 for (i = 0; i < ARRAY_SIZE(sample_freq_avail); i++)
327 if (lval == sample_freq_avail[i]) {
328 mutex_lock(&indio_dev->mlock);
329 st->mode &= ~AD7793_MODE_RATE(-1);
330 st->mode |= AD7793_MODE_RATE(i);
331 ad_sd_write_reg(&st->sd, AD7793_REG_MODE,
332 sizeof(st->mode), st->mode);
333 mutex_unlock(&indio_dev->mlock);
334 ret = 0;
335 }
336
337 return ret ? ret : len;
338}
339
340static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
341 ad7793_read_frequency,
342 ad7793_write_frequency);
343
344static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
345 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
346
347static ssize_t ad7793_show_scale_available(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
350 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
351 struct ad7793_state *st = iio_priv(indio_dev);
352 int i, len = 0;
353
354 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
355 len += sprintf(buf + len, "%d.%09u ", st->scale_avail[i][0],
356 st->scale_avail[i][1]);
357
358 len += sprintf(buf + len, "\n");
359
360 return len;
361}
362
363static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available,
364 in_voltage-voltage_scale_available, S_IRUGO,
365 ad7793_show_scale_available, NULL, 0);
366
367static struct attribute *ad7793_attributes[] = {
368 &iio_dev_attr_sampling_frequency.dev_attr.attr,
369 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
370 &iio_dev_attr_in_m_in_scale_available.dev_attr.attr,
371 NULL
372};
373
374static const struct attribute_group ad7793_attribute_group = {
375 .attrs = ad7793_attributes,
376};
377
378static int ad7793_read_raw(struct iio_dev *indio_dev,
379 struct iio_chan_spec const *chan,
380 int *val,
381 int *val2,
382 long m)
383{
384 struct ad7793_state *st = iio_priv(indio_dev);
385 int ret;
386 unsigned long long scale_uv;
387 bool unipolar = !!(st->conf & AD7793_CONF_UNIPOLAR);
388
389 switch (m) {
390 case IIO_CHAN_INFO_RAW:
391 ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
392 if (ret < 0)
393 return ret;
394
395 return IIO_VAL_INT;
396
397 case IIO_CHAN_INFO_SCALE:
398 switch (chan->type) {
399 case IIO_VOLTAGE:
400 if (chan->differential) {
401 *val = st->
402 scale_avail[(st->conf >> 8) & 0x7][0];
403 *val2 = st->
404 scale_avail[(st->conf >> 8) & 0x7][1];
405 return IIO_VAL_INT_PLUS_NANO;
406 } else {
407 /* 1170mV / 2^23 * 6 */
408 scale_uv = (1170ULL * 1000000000ULL * 6ULL);
409 }
410 break;
411 case IIO_TEMP:
412 /* 1170mV / 0.81 mV/C / 2^23 */
413 scale_uv = 1444444444444444ULL;
414 break;
415 default:
416 return -EINVAL;
417 }
418
419 scale_uv >>= (chan->scan_type.realbits - (unipolar ? 0 : 1));
420 *val = 0;
421 *val2 = scale_uv;
422 return IIO_VAL_INT_PLUS_NANO;
423 case IIO_CHAN_INFO_OFFSET:
424 if (!unipolar)
425 *val = -(1 << (chan->scan_type.realbits - 1));
426 else
427 *val = 0;
428
429 /* Kelvin to Celsius */
430 if (chan->type == IIO_TEMP) {
431 unsigned long long offset;
432 unsigned int shift;
433
434 shift = chan->scan_type.realbits - (unipolar ? 0 : 1);
435 offset = 273ULL << shift;
436 do_div(offset, 1444);
437 *val -= offset;
438 }
439 return IIO_VAL_INT;
440 }
441 return -EINVAL;
442}
443
444static int ad7793_write_raw(struct iio_dev *indio_dev,
445 struct iio_chan_spec const *chan,
446 int val,
447 int val2,
448 long mask)
449{
450 struct ad7793_state *st = iio_priv(indio_dev);
451 int ret, i;
452 unsigned int tmp;
453
454 mutex_lock(&indio_dev->mlock);
455 if (iio_buffer_enabled(indio_dev)) {
456 mutex_unlock(&indio_dev->mlock);
457 return -EBUSY;
458 }
459
460 switch (mask) {
461 case IIO_CHAN_INFO_SCALE:
462 ret = -EINVAL;
463 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
464 if (val2 == st->scale_avail[i][1]) {
465 ret = 0;
466 tmp = st->conf;
467 st->conf &= ~AD7793_CONF_GAIN(-1);
468 st->conf |= AD7793_CONF_GAIN(i);
469
470 if (tmp == st->conf)
471 break;
472
473 ad_sd_write_reg(&st->sd, AD7793_REG_CONF,
474 sizeof(st->conf), st->conf);
475 ad7793_calibrate_all(st);
476 break;
477 }
478 break;
479 default:
480 ret = -EINVAL;
481 }
482
483 mutex_unlock(&indio_dev->mlock);
484 return ret;
485}
486
487static int ad7793_write_raw_get_fmt(struct iio_dev *indio_dev,
488 struct iio_chan_spec const *chan,
489 long mask)
490{
491 return IIO_VAL_INT_PLUS_NANO;
492}
493
494static const struct iio_info ad7793_info = {
495 .read_raw = &ad7793_read_raw,
496 .write_raw = &ad7793_write_raw,
497 .write_raw_get_fmt = &ad7793_write_raw_get_fmt,
498 .attrs = &ad7793_attribute_group,
499 .validate_trigger = ad_sd_validate_trigger,
500 .driver_module = THIS_MODULE,
501};
502
503#define DECLARE_AD7793_CHANNELS(_name, _b, _sb, _s) \
504const struct iio_chan_spec _name##_channels[] = { \
505 AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), (_s)), \
506 AD_SD_DIFF_CHANNEL(1, 1, 1, AD7793_CH_AIN2P_AIN2M, (_b), (_sb), (_s)), \
507 AD_SD_DIFF_CHANNEL(2, 2, 2, AD7793_CH_AIN3P_AIN3M, (_b), (_sb), (_s)), \
508 AD_SD_SHORTED_CHANNEL(3, 0, AD7793_CH_AIN1M_AIN1M, (_b), (_sb), (_s)), \
509 AD_SD_TEMP_CHANNEL(4, AD7793_CH_TEMP, (_b), (_sb), (_s)), \
510 AD_SD_SUPPLY_CHANNEL(5, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), (_s)), \
511 IIO_CHAN_SOFT_TIMESTAMP(6), \
512}
513
514#define DECLARE_AD7795_CHANNELS(_name, _b, _sb) \
515const struct iio_chan_spec _name##_channels[] = { \
516 AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), 0), \
517 AD_SD_DIFF_CHANNEL(1, 1, 1, AD7793_CH_AIN2P_AIN2M, (_b), (_sb), 0), \
518 AD_SD_DIFF_CHANNEL(2, 2, 2, AD7793_CH_AIN3P_AIN3M, (_b), (_sb), 0), \
519 AD_SD_DIFF_CHANNEL(3, 3, 3, AD7795_CH_AIN4P_AIN4M, (_b), (_sb), 0), \
520 AD_SD_DIFF_CHANNEL(4, 4, 4, AD7795_CH_AIN5P_AIN5M, (_b), (_sb), 0), \
521 AD_SD_DIFF_CHANNEL(5, 5, 5, AD7795_CH_AIN6P_AIN6M, (_b), (_sb), 0), \
522 AD_SD_SHORTED_CHANNEL(6, 0, AD7795_CH_AIN1M_AIN1M, (_b), (_sb), 0), \
523 AD_SD_TEMP_CHANNEL(7, AD7793_CH_TEMP, (_b), (_sb), 0), \
524 AD_SD_SUPPLY_CHANNEL(8, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), 0), \
525 IIO_CHAN_SOFT_TIMESTAMP(9), \
526}
527
528static DECLARE_AD7793_CHANNELS(ad7785, 20, 32, 4);
529static DECLARE_AD7793_CHANNELS(ad7792, 16, 32, 0);
530static DECLARE_AD7793_CHANNELS(ad7793, 24, 32, 0);
531static DECLARE_AD7795_CHANNELS(ad7794, 16, 32);
532static DECLARE_AD7795_CHANNELS(ad7795, 24, 32);
533
534static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
535 [ID_AD7785] = {
536 .id = AD7785_ID,
537 .channels = ad7785_channels,
538 .num_channels = ARRAY_SIZE(ad7785_channels),
539 },
540 [ID_AD7792] = {
541 .id = AD7792_ID,
542 .channels = ad7792_channels,
543 .num_channels = ARRAY_SIZE(ad7792_channels),
544 },
545 [ID_AD7793] = {
546 .id = AD7793_ID,
547 .channels = ad7793_channels,
548 .num_channels = ARRAY_SIZE(ad7793_channels),
549 },
550 [ID_AD7794] = {
551 .id = AD7794_ID,
552 .channels = ad7794_channels,
553 .num_channels = ARRAY_SIZE(ad7794_channels),
554 },
555 [ID_AD7795] = {
556 .id = AD7795_ID,
557 .channels = ad7795_channels,
558 .num_channels = ARRAY_SIZE(ad7795_channels),
559 },
560};
561
562static int ad7793_probe(struct spi_device *spi)
563{
564 const struct ad7793_platform_data *pdata = spi->dev.platform_data;
565 struct ad7793_state *st;
566 struct iio_dev *indio_dev;
567 int ret, vref_mv = 0;
568
569 if (!pdata) {
570 dev_err(&spi->dev, "no platform data?\n");
571 return -ENODEV;
572 }
573
574 if (!spi->irq) {
575 dev_err(&spi->dev, "no IRQ?\n");
576 return -ENODEV;
577 }
578
579 indio_dev = iio_device_alloc(sizeof(*st));
580 if (indio_dev == NULL)
581 return -ENOMEM;
582
583 st = iio_priv(indio_dev);
584
585 ad_sd_init(&st->sd, indio_dev, spi, &ad7793_sigma_delta_info);
586
587 if (pdata->refsel != AD7793_REFSEL_INTERNAL) {
588 st->reg = regulator_get(&spi->dev, "refin");
589 if (IS_ERR(st->reg)) {
590 ret = PTR_ERR(st->reg);
591 goto error_device_free;
592 }
593
594 ret = regulator_enable(st->reg);
595 if (ret)
596 goto error_put_reg;
597
598 vref_mv = regulator_get_voltage(st->reg);
599 if (vref_mv < 0) {
600 ret = vref_mv;
601 goto error_disable_reg;
602 }
603
604 vref_mv /= 1000;
605 } else {
606 vref_mv = 1170; /* Build-in ref */
607 }
608
609 st->chip_info =
610 &ad7793_chip_info_tbl[spi_get_device_id(spi)->driver_data];
611
612 spi_set_drvdata(spi, indio_dev);
613
614 indio_dev->dev.parent = &spi->dev;
615 indio_dev->name = spi_get_device_id(spi)->name;
616 indio_dev->modes = INDIO_DIRECT_MODE;
617 indio_dev->channels = st->chip_info->channels;
618 indio_dev->num_channels = st->chip_info->num_channels;
619 indio_dev->info = &ad7793_info;
620
621 ret = ad_sd_setup_buffer_and_trigger(indio_dev);
622 if (ret)
623 goto error_disable_reg;
624
625 ret = ad7793_setup(indio_dev, pdata, vref_mv);
626 if (ret)
627 goto error_remove_trigger;
628
629 ret = iio_device_register(indio_dev);
630 if (ret)
631 goto error_remove_trigger;
632
633 return 0;
634
635error_remove_trigger:
636 ad_sd_cleanup_buffer_and_trigger(indio_dev);
637error_disable_reg:
638 if (pdata->refsel != AD7793_REFSEL_INTERNAL)
639 regulator_disable(st->reg);
640error_put_reg:
641 if (pdata->refsel != AD7793_REFSEL_INTERNAL)
642 regulator_put(st->reg);
643error_device_free:
644 iio_device_free(indio_dev);
645
646 return ret;
647}
648
649static int ad7793_remove(struct spi_device *spi)
650{
651 const struct ad7793_platform_data *pdata = spi->dev.platform_data;
652 struct iio_dev *indio_dev = spi_get_drvdata(spi);
653 struct ad7793_state *st = iio_priv(indio_dev);
654
655 iio_device_unregister(indio_dev);
656 ad_sd_cleanup_buffer_and_trigger(indio_dev);
657
658 if (pdata->refsel != AD7793_REFSEL_INTERNAL) {
659 regulator_disable(st->reg);
660 regulator_put(st->reg);
661 }
662
663 iio_device_free(indio_dev);
664
665 return 0;
666}
667
668static const struct spi_device_id ad7793_id[] = {
669 {"ad7785", ID_AD7785},
670 {"ad7792", ID_AD7792},
671 {"ad7793", ID_AD7793},
672 {"ad7794", ID_AD7794},
673 {"ad7795", ID_AD7795},
674 {}
675};
676MODULE_DEVICE_TABLE(spi, ad7793_id);
677
678static struct spi_driver ad7793_driver = {
679 .driver = {
680 .name = "ad7793",
681 .owner = THIS_MODULE,
682 },
683 .probe = ad7793_probe,
684 .remove = ad7793_remove,
685 .id_table = ad7793_id,
686};
687module_spi_driver(ad7793_driver);
688
689MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
690MODULE_DESCRIPTION("Analog Devices AD7793 and simialr ADCs");
691MODULE_LICENSE("GPL v2");