11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
18 #include <linux/sched.h>
20 #include <linux/module.h>
32 #define AD7791_REG_COMM 0x0
33 #define AD7791_REG_STATUS 0x0
34 #define AD7791_REG_MODE 0x1
35 #define AD7791_REG_FILTER 0x2
36 #define AD7791_REG_DATA 0x3
38 #define AD7791_MODE_CONTINUOUS 0x00
39 #define AD7791_MODE_SINGLE 0x02
40 #define AD7791_MODE_POWERDOWN 0x03
42 #define AD7791_CH_AIN1P_AIN1N 0x00
43 #define AD7791_CH_AIN2 0x01
44 #define AD7791_CH_AIN1N_AIN1N 0x02
45 #define AD7791_CH_AVDD_MONITOR 0x03
47 #define AD7791_FILTER_CLK_DIV_1 (0x0 << 4)
48 #define AD7791_FILTER_CLK_DIV_2 (0x1 << 4)
49 #define AD7791_FILTER_CLK_DIV_4 (0x2 << 4)
50 #define AD7791_FILTER_CLK_DIV_8 (0x3 << 4)
51 #define AD7791_FILTER_CLK_MASK (0x3 << 4)
52 #define AD7791_FILTER_RATE_120 0x0
53 #define AD7791_FILTER_RATE_100 0x1
54 #define AD7791_FILTER_RATE_33_3 0x2
55 #define AD7791_FILTER_RATE_20 0x3
56 #define AD7791_FILTER_RATE_16_6 0x4
57 #define AD7791_FILTER_RATE_16_7 0x5
58 #define AD7791_FILTER_RATE_13_3 0x6
59 #define AD7791_FILTER_RATE_9_5 0x7
60 #define AD7791_FILTER_RATE_MASK 0x7
62 #define AD7791_MODE_BUFFER BIT(1)
63 #define AD7791_MODE_UNIPOLAR BIT(2)
64 #define AD7791_MODE_BURNOUT BIT(3)
65 #define AD7791_MODE_SEL_MASK (0x3 << 6)
66 #define AD7791_MODE_SEL(x) ((x) << 6)
68 #define DECLARE_AD7787_CHANNELS(name, bits, storagebits) \
69 const struct iio_chan_spec name[] = { \
70 AD_SD_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
71 (bits), (storagebits), 0), \
72 AD_SD_CHANNEL(1, 1, AD7791_CH_AIN2, (bits), (storagebits), 0), \
73 AD_SD_SHORTED_CHANNEL(2, 0, AD7791_CH_AIN1N_AIN1N, \
74 (bits), (storagebits), 0), \
75 AD_SD_SUPPLY_CHANNEL(3, 2, AD7791_CH_AVDD_MONITOR, \
76 (bits), (storagebits), 0), \
77 IIO_CHAN_SOFT_TIMESTAMP(4), \
80 #define DECLARE_AD7791_CHANNELS(name, bits, storagebits) \
81 const struct iio_chan_spec name[] = { \
82 AD_SD_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
83 (bits), (storagebits), 0), \
84 AD_SD_SHORTED_CHANNEL(1, 0, AD7791_CH_AIN1N_AIN1N, \
85 (bits), (storagebits), 0), \
86 AD_SD_SUPPLY_CHANNEL(2, 1, AD7791_CH_AVDD_MONITOR, \
87 (bits), (storagebits), 0), \
88 IIO_CHAN_SOFT_TIMESTAMP(3), \
118 .channels = ad7787_channels,
124 .channels = ad7790_channels,
129 .channels = ad7791_channels,
134 .channels = ad7790_channels,
140 .channels = ad7791_channels,
193 .set_channel = ad7791_set_channel,
194 .set_mode = ad7791_set_mode,
195 .has_registers =
true,
200 static int ad7791_read_raw(
struct iio_dev *indio_dev,
205 unsigned long long scale_pv;
218 *val = -(1 << (chan->
scan_type.realbits - 1));
223 scale_pv = 5850000000000ULL;
230 scale_pv = (
unsigned long long)voltage_uv * 1000000;
235 scale_pv >>= chan->
scan_type.realbits - 1;
236 *val2 =
do_div(scale_pv, 1000000000);
245 static const char *
const ad7791_sample_freq_avail[] = {
259 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
263 return sprintf(buf,
"%s\n", ad7791_sample_freq_avail[rate]);
269 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
274 if (iio_buffer_enabled(indio_dev)) {
282 for (i = 0; i <
ARRAY_SIZE(ad7791_sample_freq_avail); i++) {
283 if (
sysfs_streq(ad7791_sample_freq_avail[i], buf)) {
296 return ret ? ret : len;
300 ad7791_read_frequency,
301 ad7791_write_frequency);
305 static struct attribute *ad7791_attributes[] = {
306 &iio_dev_attr_sampling_frequency.dev_attr.attr,
307 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
312 .attrs = ad7791_attributes,
315 static const struct iio_info ad7791_info = {
316 .read_raw = &ad7791_read_raw,
317 .attrs = &ad7791_attribute_group,
322 static const struct iio_info ad7791_no_filter_info = {
323 .read_raw = &ad7791_read_raw,
368 st = iio_priv(indio_dev);
371 if (IS_ERR(st->
reg)) {
372 ret = PTR_ERR(st->
reg);
381 ad_sd_init(&st->
sd, indio_dev, spi, &ad7791_sigma_delta_info);
383 spi_set_drvdata(spi, indio_dev);
385 indio_dev->
dev.parent = &spi->
dev;
391 indio_dev->
info = &ad7791_info;
393 indio_dev->
info = &ad7791_no_filter_info;
397 goto error_disable_reg;
399 ret = ad7791_setup(st, pdata);
401 goto error_remove_trigger;
405 goto error_remove_trigger;
409 error_remove_trigger:
423 struct iio_dev *indio_dev = spi_get_drvdata(spi);
452 .probe = ad7791_probe,
454 .id_table = ad7791_spi_ids,