1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD7787/AD7788/AD7789/AD7790/AD7791 SPI ADC driver
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
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>
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>
29 #include <linux/platform_data/ad7791.h>
31 #define AD7791_REG_COMM 0x0 /* For writes */
32 #define AD7791_REG_STATUS 0x0 /* For reads */
33 #define AD7791_REG_MODE 0x1
34 #define AD7791_REG_FILTER 0x2
35 #define AD7791_REG_DATA 0x3
37 #define AD7791_MODE_CONTINUOUS 0x00
38 #define AD7791_MODE_SINGLE 0x02
39 #define AD7791_MODE_POWERDOWN 0x03
41 #define AD7791_CH_AIN1P_AIN1N 0x00
42 #define AD7791_CH_AIN2 0x01
43 #define AD7791_CH_AIN1N_AIN1N 0x02
44 #define AD7791_CH_AVDD_MONITOR 0x03
46 #define AD7791_FILTER_CLK_DIV_1 (0x0 << 4)
47 #define AD7791_FILTER_CLK_DIV_2 (0x1 << 4)
48 #define AD7791_FILTER_CLK_DIV_4 (0x2 << 4)
49 #define AD7791_FILTER_CLK_DIV_8 (0x3 << 4)
50 #define AD7791_FILTER_CLK_MASK (0x3 << 4)
51 #define AD7791_FILTER_RATE_120 0x0
52 #define AD7791_FILTER_RATE_100 0x1
53 #define AD7791_FILTER_RATE_33_3 0x2
54 #define AD7791_FILTER_RATE_20 0x3
55 #define AD7791_FILTER_RATE_16_6 0x4
56 #define AD7791_FILTER_RATE_16_7 0x5
57 #define AD7791_FILTER_RATE_13_3 0x6
58 #define AD7791_FILTER_RATE_9_5 0x7
59 #define AD7791_FILTER_RATE_MASK 0x7
61 #define AD7791_MODE_BUFFER BIT(1)
62 #define AD7791_MODE_UNIPOLAR BIT(2)
63 #define AD7791_MODE_BURNOUT BIT(3)
64 #define AD7791_MODE_SEL_MASK (0x3 << 6)
65 #define AD7791_MODE_SEL(x) ((x) << 6)
67 #define __AD7991_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
68 _storagebits, _shift, _extend_name, _type, _mask_all) \
71 .differential = (_channel2 == -1 ? 0 : 1), \
73 .channel = (_channel1), \
74 .channel2 = (_channel2), \
75 .address = (_address), \
76 .extend_name = (_extend_name), \
77 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
78 BIT(IIO_CHAN_INFO_OFFSET), \
79 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
80 .info_mask_shared_by_all = _mask_all, \
81 .scan_index = (_si), \
84 .realbits = (_bits), \
85 .storagebits = (_storagebits), \
87 .endianness = IIO_BE, \
91 #define AD7991_SHORTED_CHANNEL(_si, _channel, _address, _bits, \
92 _storagebits, _shift) \
93 __AD7991_CHANNEL(_si, _channel, _channel, _address, _bits, \
94 _storagebits, _shift, "shorted", IIO_VOLTAGE, \
95 BIT(IIO_CHAN_INFO_SAMP_FREQ))
97 #define AD7991_CHANNEL(_si, _channel, _address, _bits, \
98 _storagebits, _shift) \
99 __AD7991_CHANNEL(_si, _channel, -1, _address, _bits, \
100 _storagebits, _shift, NULL, IIO_VOLTAGE, \
101 BIT(IIO_CHAN_INFO_SAMP_FREQ))
103 #define AD7991_DIFF_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
104 _storagebits, _shift) \
105 __AD7991_CHANNEL(_si, _channel1, _channel2, _address, _bits, \
106 _storagebits, _shift, NULL, IIO_VOLTAGE, \
107 BIT(IIO_CHAN_INFO_SAMP_FREQ))
109 #define AD7991_SUPPLY_CHANNEL(_si, _channel, _address, _bits, _storagebits, \
111 __AD7991_CHANNEL(_si, _channel, -1, _address, _bits, \
112 _storagebits, _shift, "supply", IIO_VOLTAGE, \
113 BIT(IIO_CHAN_INFO_SAMP_FREQ))
115 #define DECLARE_AD7787_CHANNELS(name, bits, storagebits) \
116 const struct iio_chan_spec name[] = { \
117 AD7991_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
118 (bits), (storagebits), 0), \
119 AD7991_CHANNEL(1, 1, AD7791_CH_AIN2, (bits), (storagebits), 0), \
120 AD7991_SHORTED_CHANNEL(2, 0, AD7791_CH_AIN1N_AIN1N, \
121 (bits), (storagebits), 0), \
122 AD7991_SUPPLY_CHANNEL(3, 2, AD7791_CH_AVDD_MONITOR, \
123 (bits), (storagebits), 0), \
124 IIO_CHAN_SOFT_TIMESTAMP(4), \
127 #define DECLARE_AD7791_CHANNELS(name, bits, storagebits) \
128 const struct iio_chan_spec name[] = { \
129 AD7991_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
130 (bits), (storagebits), 0), \
131 AD7991_SHORTED_CHANNEL(1, 0, AD7791_CH_AIN1N_AIN1N, \
132 (bits), (storagebits), 0), \
133 AD7991_SUPPLY_CHANNEL(2, 1, AD7791_CH_AVDD_MONITOR, \
134 (bits), (storagebits), 0), \
135 IIO_CHAN_SOFT_TIMESTAMP(3), \
138 static DECLARE_AD7787_CHANNELS(ad7787_channels
, 24, 32);
139 static DECLARE_AD7791_CHANNELS(ad7790_channels
, 16, 16);
140 static DECLARE_AD7791_CHANNELS(ad7791_channels
, 24, 32);
150 enum ad7791_chip_info_flags
{
151 AD7791_FLAG_HAS_FILTER
= (1 << 0),
152 AD7791_FLAG_HAS_BUFFER
= (1 << 1),
153 AD7791_FLAG_HAS_UNIPOLAR
= (1 << 2),
154 AD7791_FLAG_HAS_BURNOUT
= (1 << 3),
157 struct ad7791_chip_info
{
158 const struct iio_chan_spec
*channels
;
159 unsigned int num_channels
;
160 enum ad7791_chip_info_flags flags
;
163 static const struct ad7791_chip_info ad7791_chip_infos
[] = {
165 .channels
= ad7787_channels
,
166 .num_channels
= ARRAY_SIZE(ad7787_channels
),
167 .flags
= AD7791_FLAG_HAS_FILTER
| AD7791_FLAG_HAS_BUFFER
|
168 AD7791_FLAG_HAS_UNIPOLAR
| AD7791_FLAG_HAS_BURNOUT
,
171 .channels
= ad7790_channels
,
172 .num_channels
= ARRAY_SIZE(ad7790_channels
),
173 .flags
= AD7791_FLAG_HAS_UNIPOLAR
,
176 .channels
= ad7791_channels
,
177 .num_channels
= ARRAY_SIZE(ad7791_channels
),
178 .flags
= AD7791_FLAG_HAS_UNIPOLAR
,
181 .channels
= ad7790_channels
,
182 .num_channels
= ARRAY_SIZE(ad7790_channels
),
183 .flags
= AD7791_FLAG_HAS_FILTER
| AD7791_FLAG_HAS_BUFFER
|
184 AD7791_FLAG_HAS_BURNOUT
,
187 .channels
= ad7791_channels
,
188 .num_channels
= ARRAY_SIZE(ad7791_channels
),
189 .flags
= AD7791_FLAG_HAS_FILTER
| AD7791_FLAG_HAS_BUFFER
|
190 AD7791_FLAG_HAS_UNIPOLAR
| AD7791_FLAG_HAS_BURNOUT
,
194 struct ad7791_state
{
195 struct ad_sigma_delta sd
;
199 struct regulator
*reg
;
200 const struct ad7791_chip_info
*info
;
203 static const int ad7791_sample_freq_avail
[8][2] = {
204 [AD7791_FILTER_RATE_120
] = { 120, 0 },
205 [AD7791_FILTER_RATE_100
] = { 100, 0 },
206 [AD7791_FILTER_RATE_33_3
] = { 33, 300000 },
207 [AD7791_FILTER_RATE_20
] = { 20, 0 },
208 [AD7791_FILTER_RATE_16_6
] = { 16, 600000 },
209 [AD7791_FILTER_RATE_16_7
] = { 16, 700000 },
210 [AD7791_FILTER_RATE_13_3
] = { 13, 300000 },
211 [AD7791_FILTER_RATE_9_5
] = { 9, 500000 },
214 static struct ad7791_state
*ad_sigma_delta_to_ad7791(struct ad_sigma_delta
*sd
)
216 return container_of(sd
, struct ad7791_state
, sd
);
219 static int ad7791_set_channel(struct ad_sigma_delta
*sd
, unsigned int channel
)
221 ad_sd_set_comm(sd
, channel
);
226 static int ad7791_set_mode(struct ad_sigma_delta
*sd
,
227 enum ad_sigma_delta_mode mode
)
229 struct ad7791_state
*st
= ad_sigma_delta_to_ad7791(sd
);
232 case AD_SD_MODE_CONTINUOUS
:
233 mode
= AD7791_MODE_CONTINUOUS
;
235 case AD_SD_MODE_SINGLE
:
236 mode
= AD7791_MODE_SINGLE
;
238 case AD_SD_MODE_IDLE
:
239 case AD_SD_MODE_POWERDOWN
:
240 mode
= AD7791_MODE_POWERDOWN
;
244 st
->mode
&= ~AD7791_MODE_SEL_MASK
;
245 st
->mode
|= AD7791_MODE_SEL(mode
);
247 return ad_sd_write_reg(sd
, AD7791_REG_MODE
, sizeof(st
->mode
), st
->mode
);
250 static const struct ad_sigma_delta_info ad7791_sigma_delta_info
= {
251 .set_channel
= ad7791_set_channel
,
252 .set_mode
= ad7791_set_mode
,
253 .has_registers
= true,
256 .irq_flags
= IRQF_TRIGGER_LOW
,
259 static int ad7791_read_raw(struct iio_dev
*indio_dev
,
260 const struct iio_chan_spec
*chan
, int *val
, int *val2
, long info
)
262 struct ad7791_state
*st
= iio_priv(indio_dev
);
263 bool unipolar
= !!(st
->mode
& AD7791_MODE_UNIPOLAR
);
267 case IIO_CHAN_INFO_RAW
:
268 return ad_sigma_delta_single_conversion(indio_dev
, chan
, val
);
269 case IIO_CHAN_INFO_OFFSET
:
271 * Unipolar: 0 to VREF
272 * Bipolar -VREF to VREF
277 *val
= -(1 << (chan
->scan_type
.realbits
- 1));
279 case IIO_CHAN_INFO_SCALE
:
280 /* The monitor channel uses an internal reference. */
281 if (chan
->address
== AD7791_CH_AVDD_MONITOR
) {
283 * The signal is attenuated by a factor of 5 and
284 * compared against a 1.17V internal reference.
290 voltage_uv
= regulator_get_voltage(st
->reg
);
294 *val
= voltage_uv
/ 1000;
297 *val2
= chan
->scan_type
.realbits
;
299 *val2
= chan
->scan_type
.realbits
- 1;
301 return IIO_VAL_FRACTIONAL_LOG2
;
302 case IIO_CHAN_INFO_SAMP_FREQ
:
303 rate
= st
->filter
& AD7791_FILTER_RATE_MASK
;
304 *val
= ad7791_sample_freq_avail
[rate
][0];
305 *val2
= ad7791_sample_freq_avail
[rate
][1];
306 return IIO_VAL_INT_PLUS_MICRO
;
312 static int ad7791_write_raw(struct iio_dev
*indio_dev
,
313 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
315 struct ad7791_state
*st
= iio_priv(indio_dev
);
318 ret
= iio_device_claim_direct_mode(indio_dev
);
323 case IIO_CHAN_INFO_SAMP_FREQ
:
324 for (i
= 0; i
< ARRAY_SIZE(ad7791_sample_freq_avail
); i
++) {
325 if (ad7791_sample_freq_avail
[i
][0] == val
&&
326 ad7791_sample_freq_avail
[i
][1] == val2
)
330 if (i
== ARRAY_SIZE(ad7791_sample_freq_avail
)) {
335 st
->filter
&= ~AD7791_FILTER_RATE_MASK
;
337 ad_sd_write_reg(&st
->sd
, AD7791_REG_FILTER
,
345 iio_device_release_direct_mode(indio_dev
);
349 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("120 100 33.3 20 16.7 16.6 13.3 9.5");
351 static struct attribute
*ad7791_attributes
[] = {
352 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
356 static const struct attribute_group ad7791_attribute_group
= {
357 .attrs
= ad7791_attributes
,
360 static const struct iio_info ad7791_info
= {
361 .read_raw
= &ad7791_read_raw
,
362 .write_raw
= &ad7791_write_raw
,
363 .attrs
= &ad7791_attribute_group
,
364 .validate_trigger
= ad_sd_validate_trigger
,
367 static const struct iio_info ad7791_no_filter_info
= {
368 .read_raw
= &ad7791_read_raw
,
369 .write_raw
= &ad7791_write_raw
,
370 .validate_trigger
= ad_sd_validate_trigger
,
373 static int ad7791_setup(struct ad7791_state
*st
,
374 struct ad7791_platform_data
*pdata
)
376 /* Set to poweron-reset default values */
377 st
->mode
= AD7791_MODE_BUFFER
;
378 st
->filter
= AD7791_FILTER_RATE_16_6
;
383 if ((st
->info
->flags
& AD7791_FLAG_HAS_BUFFER
) && !pdata
->buffered
)
384 st
->mode
&= ~AD7791_MODE_BUFFER
;
386 if ((st
->info
->flags
& AD7791_FLAG_HAS_BURNOUT
) &&
387 pdata
->burnout_current
)
388 st
->mode
|= AD7791_MODE_BURNOUT
;
390 if ((st
->info
->flags
& AD7791_FLAG_HAS_UNIPOLAR
) && pdata
->unipolar
)
391 st
->mode
|= AD7791_MODE_UNIPOLAR
;
393 return ad_sd_write_reg(&st
->sd
, AD7791_REG_MODE
, sizeof(st
->mode
),
397 static int ad7791_probe(struct spi_device
*spi
)
399 struct ad7791_platform_data
*pdata
= spi
->dev
.platform_data
;
400 struct iio_dev
*indio_dev
;
401 struct ad7791_state
*st
;
405 dev_err(&spi
->dev
, "Missing IRQ.\n");
409 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
413 st
= iio_priv(indio_dev
);
415 st
->reg
= devm_regulator_get(&spi
->dev
, "refin");
417 return PTR_ERR(st
->reg
);
419 ret
= regulator_enable(st
->reg
);
423 st
->info
= &ad7791_chip_infos
[spi_get_device_id(spi
)->driver_data
];
424 ad_sd_init(&st
->sd
, indio_dev
, spi
, &ad7791_sigma_delta_info
);
426 spi_set_drvdata(spi
, indio_dev
);
428 indio_dev
->name
= spi_get_device_id(spi
)->name
;
429 indio_dev
->modes
= INDIO_DIRECT_MODE
;
430 indio_dev
->channels
= st
->info
->channels
;
431 indio_dev
->num_channels
= st
->info
->num_channels
;
432 if (st
->info
->flags
& AD7791_FLAG_HAS_FILTER
)
433 indio_dev
->info
= &ad7791_info
;
435 indio_dev
->info
= &ad7791_no_filter_info
;
437 ret
= ad_sd_setup_buffer_and_trigger(indio_dev
);
439 goto error_disable_reg
;
441 ret
= ad7791_setup(st
, pdata
);
443 goto error_remove_trigger
;
445 ret
= iio_device_register(indio_dev
);
447 goto error_remove_trigger
;
451 error_remove_trigger
:
452 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
454 regulator_disable(st
->reg
);
459 static int ad7791_remove(struct spi_device
*spi
)
461 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
462 struct ad7791_state
*st
= iio_priv(indio_dev
);
464 iio_device_unregister(indio_dev
);
465 ad_sd_cleanup_buffer_and_trigger(indio_dev
);
467 regulator_disable(st
->reg
);
472 static const struct spi_device_id ad7791_spi_ids
[] = {
473 { "ad7787", AD7787
},
474 { "ad7788", AD7788
},
475 { "ad7789", AD7789
},
476 { "ad7790", AD7790
},
477 { "ad7791", AD7791
},
480 MODULE_DEVICE_TABLE(spi
, ad7791_spi_ids
);
482 static struct spi_driver ad7791_driver
= {
486 .probe
= ad7791_probe
,
487 .remove
= ad7791_remove
,
488 .id_table
= ad7791_spi_ids
,
490 module_spi_driver(ad7791_driver
);
492 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
493 MODULE_DESCRIPTION("Analog Devices AD7787/AD7788/AD7789/AD7790/AD7791 ADC driver");
494 MODULE_LICENSE("GPL v2");