11 #include <linux/device.h>
12 #include <linux/kernel.h>
14 #include <linux/slab.h>
17 #include <linux/module.h>
24 #define AD5505_BITS 12
25 #define AD5504_RES_MASK ((1 << (AD5505_BITS)) - 1)
27 #define AD5504_CMD_READ (1 << 15)
28 #define AD5504_CMD_WRITE (0 << 15)
29 #define AD5504_ADDR(addr) ((addr) << 12)
32 #define AD5504_ADDR_NOOP 0
33 #define AD5504_ADDR_DAC(x) ((x) + 1)
34 #define AD5504_ADDR_ALL_DAC 5
35 #define AD5504_ADDR_CTRL 7
38 #define AD5504_DAC_PWR(ch) ((ch) << 2)
39 #define AD5504_DAC_PWRDWN_MODE(mode) ((mode) << 6)
40 #define AD5504_DAC_PWRDN_20K 0
41 #define AD5504_DAC_PWRDN_3STATE 1
75 return spi_write(spi, (
u8 *)&tmp, 2);
91 spi_message_add_tail(&t, &
m);
100 static int ad5504_read_raw(
struct iio_dev *indio_dev,
107 unsigned long scale_uv;
112 ret = ad5504_spi_read(st->
spi, chan->
address);
121 *val = scale_uv / 1000;
122 *val2 = (scale_uv % 1000) * 1000;
129 static int ad5504_write_raw(
struct iio_dev *indio_dev,
140 if (val >= (1 << chan->
scan_type.realbits) || val < 0)
143 return ad5504_spi_write(st->
spi, chan->
address, val);
151 static const char *
const ad5504_powerdown_modes[] = {
156 static int ad5504_get_powerdown_mode(
struct iio_dev *indio_dev,
164 static int ad5504_set_powerdown_mode(
struct iio_dev *indio_dev,
174 static const struct iio_enum ad5504_powerdown_mode_enum = {
175 .items = ad5504_powerdown_modes,
176 .num_items =
ARRAY_SIZE(ad5504_powerdown_modes),
177 .get = ad5504_get_powerdown_mode,
178 .set = ad5504_set_powerdown_mode,
181 static ssize_t ad5504_read_dac_powerdown(
struct iio_dev *indio_dev,
190 static ssize_t ad5504_write_dac_powerdown(
struct iio_dev *indio_dev,
214 return ret ? ret : len;
220 static struct attribute *ad5504_ev_attributes[] = {
221 &iio_const_attr_temp0_thresh_rising_value.dev_attr.attr,
222 &iio_const_attr_temp0_thresh_rising_en.dev_attr.attr,
227 .attrs = ad5504_ev_attributes,
231 static irqreturn_t ad5504_event_handler(
int irq,
void *
private)
243 static const struct iio_info ad5504_info = {
244 .write_raw = ad5504_write_raw,
245 .read_raw = ad5504_read_raw,
246 .event_attrs = &ad5504_ev_attribute_group,
253 .read = ad5504_read_dac_powerdown,
254 .write = ad5504_write_dac_powerdown,
256 IIO_ENUM(
"powerdown_mode",
true, &ad5504_powerdown_mode_enum),
261 #define AD5504_CHANNEL(_chan) { \
262 .type = IIO_VOLTAGE, \
265 .channel = (_chan), \
266 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
267 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
268 .address = AD5504_ADDR_DAC(_chan), \
269 .scan_type = IIO_ST('u', 12, 16, 0), \
270 .ext_info = ad5504_ext_info, \
286 int ret, voltage_uv = 0;
289 if (indio_dev ==
NULL) {
302 spi_set_drvdata(spi, indio_dev);
303 st = iio_priv(indio_dev);
305 st->
vref_mv = voltage_uv / 1000;
309 dev_warn(&spi->
dev,
"reference voltage unspecified\n");
313 indio_dev->
dev.parent = &spi->
dev;
315 indio_dev->
info = &ad5504_info;
320 indio_dev->
channels = ad5504_channels;
326 &ad5504_event_handler,
331 goto error_disable_reg;
357 struct iio_dev *indio_dev = spi_get_drvdata(spi);
364 if (!IS_ERR(st->
reg)) {
385 .probe = ad5504_probe,
387 .id_table = ad5504_id,