10 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/slab.h>
22 #define AD5360_CMD(x) ((x) << 22)
23 #define AD5360_ADDR(x) ((x) << 16)
25 #define AD5360_READBACK_TYPE(x) ((x) << 13)
26 #define AD5360_READBACK_ADDR(x) ((x) << 7)
28 #define AD5360_CHAN_ADDR(chan) ((chan) + 0x8)
30 #define AD5360_CMD_WRITE_DATA 0x3
31 #define AD5360_CMD_WRITE_OFFSET 0x2
32 #define AD5360_CMD_WRITE_GAIN 0x1
33 #define AD5360_CMD_SPECIAL_FUNCTION 0x0
36 #define AD5360_REG_SF_NOP 0x0
37 #define AD5360_REG_SF_CTRL 0x1
38 #define AD5360_REG_SF_OFS(x) (0x2 + (x))
39 #define AD5360_REG_SF_READBACK 0x5
41 #define AD5360_SF_CTRL_PWR_DOWN BIT(0)
43 #define AD5360_READBACK_X1A 0x0
44 #define AD5360_READBACK_X1B 0x1
45 #define AD5360_READBACK_OFFSET 0x2
46 #define AD5360_READBACK_GAIN 0x3
47 #define AD5360_READBACK_SF 0x4
101 #define AD5360_CHANNEL(bits) { \
102 .type = IIO_VOLTAGE, \
105 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
106 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
107 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
108 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
109 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
110 .scan_type = IIO_ST('u', (bits), 16, 16 - (bits)) \
117 .channels_per_group = 8,
123 .channels_per_group = 8,
129 .channels_per_group = 4,
135 .channels_per_group = 4,
141 .channels_per_group = 8,
147 .channels_per_group = 8,
153 .channels_per_group = 8,
159 .channels_per_group = 8,
164 static unsigned int ad5360_get_channel_vref_index(
struct ad5360_state *
st,
171 i = channel / st->
chip_info->channels_per_group;
178 static int ad5360_get_channel_vref(
struct ad5360_state *st,
179 unsigned int channel)
181 unsigned int i = ad5360_get_channel_vref_index(st, channel);
187 static int ad5360_write_unlocked(
struct iio_dev *indio_dev,
188 unsigned int cmd,
unsigned int addr,
unsigned int val,
197 return spi_write(st->
spi, &st->data[0].
d8[1], 3);
200 static int ad5360_write(
struct iio_dev *indio_dev,
unsigned int cmd,
201 unsigned int addr,
unsigned int val,
unsigned int shift)
206 ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
212 static int ad5360_read(
struct iio_dev *indio_dev,
unsigned int type,
220 .tx_buf = &st->data[0].
d8[1],
224 .rx_buf = &st->data[1].
d8[1],
229 spi_message_init(&
m);
230 spi_message_add_tail(&t[0], &
m);
231 spi_message_add_tail(&t[1], &
m);
253 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
259 static int ad5360_update_ctrl(
struct iio_dev *indio_dev,
unsigned int set,
278 static ssize_t ad5360_write_dac_powerdown(
struct device *dev,
281 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
294 return ret ? ret : len;
299 ad5360_read_dac_powerdown,
300 ad5360_write_dac_powerdown, 0);
302 static struct attribute *ad5360_attributes[] = {
303 &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
308 .attrs = ad5360_attributes,
311 static int ad5360_write_raw(
struct iio_dev *indio_dev,
318 int max_val = (1 << chan->
scan_type.realbits);
319 unsigned int ofs_index;
323 if (val >= max_val || val < 0)
330 if (val >= max_val || val < 0)
337 if (val >= max_val || val < 0)
344 if (val <= -max_val || val > 0)
357 ofs_index = ad5360_get_channel_vref_index(st, chan->
channel);
367 static int ad5360_read_raw(
struct iio_dev *indio_dev,
374 unsigned int ofs_index;
388 scale_uv = ad5360_get_channel_vref(st, chan->
channel) * 4 * 100;
393 *val = scale_uv / 100000;
394 *val2 = (scale_uv % 100000) * 10;
411 ofs_index = ad5360_get_channel_vref_index(st, chan->
channel);
425 static const struct iio_info ad5360_info = {
426 .read_raw = ad5360_read_raw,
427 .write_raw = ad5360_write_raw,
428 .attrs = &ad5360_attribute_group,
432 static const char *
const ad5360_vref_name[] = {
433 "vref0",
"vref1",
"vref2"
442 channels = kcalloc(st->
chip_info->num_channels,
448 for (i = 0; i < st->
chip_info->num_channels; ++
i) {
449 channels[
i] = st->
chip_info->channel_template;
468 if (indio_dev ==
NULL) {
469 dev_err(&spi->
dev,
"Failed to allocate iio device\n");
473 st = iio_priv(indio_dev);
474 spi_set_drvdata(spi, indio_dev);
479 indio_dev->
dev.parent = &spi->
dev;
481 indio_dev->
info = &ad5360_info;
485 ret = ad5360_alloc_channels(indio_dev);
487 dev_err(&spi->
dev,
"Failed to allocate channel spec: %d\n", ret);
491 for (i = 0; i < st->
chip_info->num_vrefs; ++
i)
492 st->
vref_reg[i].supply = ad5360_vref_name[i];
497 dev_err(&spi->
dev,
"Failed to request vref regulators: %d\n", ret);
498 goto error_free_channels;
503 dev_err(&spi->
dev,
"Failed to enable vref regulators: %d\n", ret);
509 dev_err(&spi->
dev,
"Failed to register iio device: %d\n", ret);
510 goto error_disable_reg;
529 struct iio_dev *indio_dev = spi_get_drvdata(spi);
562 .probe = ad5360_probe,
564 .id_table = ad5360_ids,