10 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/slab.h>
22 #define AD5764_REG_SF_NOP 0x0
23 #define AD5764_REG_SF_CONFIG 0x1
24 #define AD5764_REG_SF_CLEAR 0x4
25 #define AD5764_REG_SF_LOAD 0x5
26 #define AD5764_REG_DATA(x) ((2 << 3) | (x))
27 #define AD5764_REG_COARSE_GAIN(x) ((3 << 3) | (x))
28 #define AD5764_REG_FINE_GAIN(x) ((4 << 3) | (x))
29 #define AD5764_REG_OFFSET(x) ((5 << 3) | (x))
31 #define AD5764_NUM_CHANNELS 4
75 #define AD5764_CHANNEL(_chan, _bits) { \
76 .type = IIO_VOLTAGE, \
81 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
82 IIO_CHAN_INFO_OFFSET_SHARED_BIT | \
83 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
84 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
85 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
86 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)) \
89 #define DECLARE_AD5764_CHANNELS(_name, _bits) \
90 const struct iio_chan_spec _name##_channels[] = { \
91 AD5764_CHANNEL(0, (_bits)), \
92 AD5764_CHANNEL(1, (_bits)), \
93 AD5764_CHANNEL(2, (_bits)), \
94 AD5764_CHANNEL(3, (_bits)), \
103 .channels = ad5744_channels,
107 .channels = ad5744_channels,
111 .channels = ad5764_channels,
115 .channels = ad5764_channels,
119 static int ad5764_write(
struct iio_dev *indio_dev,
unsigned int reg,
128 ret = spi_write(st->
spi, &st->data[0].
d8[1], 3);
134 static int ad5764_read(
struct iio_dev *indio_dev,
unsigned int reg,
142 .tx_buf = &st->data[0].
d8[1],
146 .rx_buf = &st->data[1].
d8[1],
151 spi_message_init(&
m);
152 spi_message_add_tail(&t[0], &
m);
153 spi_message_add_tail(&t[1], &
m);
184 static int ad5764_write_raw(
struct iio_dev *indio_dev,
185 struct iio_chan_spec const *chan,
int val,
int val2,
long info)
187 const int max_val = (1 << chan->
scan_type.realbits);
192 if (val >= max_val || val < 0)
197 if (val >= 128 || val < -128)
201 if (val >= 32 || val < -32)
208 reg = ad5764_chan_info_to_reg(chan, info);
209 return ad5764_write(indio_dev, reg, (
u16)val);
212 static int ad5764_get_channel_vref(
struct ad5764_state *st,
221 static int ad5764_read_raw(
struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan,
int *val,
int *val2,
long info)
225 unsigned long scale_uv;
233 ret = ad5764_read(indio_dev, reg, val);
240 ret = ad5764_read(indio_dev, reg, val);
243 *val = sign_extend32(*val, 7);
247 ret = ad5764_read(indio_dev, reg, val);
250 *val = sign_extend32(*val, 5);
254 vref = ad5764_get_channel_vref(st, chan->
channel);
258 scale_uv = (vref * 4 * 100) >> chan->
scan_type.realbits;
259 *val = scale_uv / 100000;
260 *val2 = (scale_uv % 100000) * 10;
263 *val = -(1 << chan->
scan_type.realbits) / 2;
270 static const struct iio_info ad5764_info = {
271 .read_raw = ad5764_read_raw,
272 .write_raw = ad5764_write_raw,
284 if (indio_dev ==
NULL) {
285 dev_err(&spi->
dev,
"Failed to allocate iio device\n");
289 st = iio_priv(indio_dev);
290 spi_set_drvdata(spi, indio_dev);
295 indio_dev->
dev.parent = &spi->
dev;
297 indio_dev->
info = &ad5764_info;
309 dev_err(&spi->
dev,
"Failed to request vref regulators: %d\n",
317 dev_err(&spi->
dev,
"Failed to enable vref regulators: %d\n",
325 dev_err(&spi->
dev,
"Failed to register iio device: %d\n", ret);
326 goto error_disable_reg;
345 struct iio_dev *indio_dev = spi_get_drvdata(spi);
374 .probe = ad5764_probe,
376 .id_table = ad5764_ids,