2 * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
5 * Copyright 2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/interrupt.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/module.h>
25 static int ad5791_spi_write(struct spi_device
*spi
, u8 addr
, u32 val
)
32 data
.d32
= cpu_to_be32(AD5791_CMD_WRITE
|
34 (val
& AD5791_DAC_MASK
));
36 return spi_write(spi
, &data
.d8
[1], 3);
39 static int ad5791_spi_read(struct spi_device
*spi
, u8 addr
, u32
*val
)
46 struct spi_message msg
;
47 struct spi_transfer xfers
[] = {
49 .tx_buf
= &data
[0].d8
[1],
54 .tx_buf
= &data
[1].d8
[1],
55 .rx_buf
= &data
[2].d8
[1],
61 data
[0].d32
= cpu_to_be32(AD5791_CMD_READ
|
63 data
[1].d32
= cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP
));
65 spi_message_init(&msg
);
66 spi_message_add_tail(&xfers
[0], &msg
);
67 spi_message_add_tail(&xfers
[1], &msg
);
68 ret
= spi_sync(spi
, &msg
);
70 *val
= be32_to_cpu(data
[2].d32
);
75 #define AD5791_CHAN(bits, shift) { \
76 .type = IIO_VOLTAGE, \
79 .address = AD5791_ADDR_DAC0, \
81 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | \
82 IIO_CHAN_INFO_OFFSET_SHARED_BIT, \
83 .scan_type = IIO_ST('u', bits, 24, shift) \
86 static const struct iio_chan_spec ad5791_channels
[] = {
87 [ID_AD5760
] = AD5791_CHAN(16, 4),
88 [ID_AD5780
] = AD5791_CHAN(18, 2),
89 [ID_AD5781
] = AD5791_CHAN(18, 2),
90 [ID_AD5791
] = AD5791_CHAN(20, 0)
93 static ssize_t
ad5791_read_powerdown_mode(struct device
*dev
,
94 struct device_attribute
*attr
, char *buf
)
96 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
97 struct ad5791_state
*st
= iio_priv(indio_dev
);
99 const char mode
[][14] = {"6kohm_to_gnd", "three_state"};
101 return sprintf(buf
, "%s\n", mode
[st
->pwr_down_mode
]);
104 static ssize_t
ad5791_write_powerdown_mode(struct device
*dev
,
105 struct device_attribute
*attr
,
106 const char *buf
, size_t len
)
108 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
109 struct ad5791_state
*st
= iio_priv(indio_dev
);
112 if (sysfs_streq(buf
, "6kohm_to_gnd"))
113 st
->pwr_down_mode
= AD5791_DAC_PWRDN_6K
;
114 else if (sysfs_streq(buf
, "three_state"))
115 st
->pwr_down_mode
= AD5791_DAC_PWRDN_3STATE
;
119 return ret
? ret
: len
;
122 static ssize_t
ad5791_read_dac_powerdown(struct device
*dev
,
123 struct device_attribute
*attr
,
126 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
127 struct ad5791_state
*st
= iio_priv(indio_dev
);
129 return sprintf(buf
, "%d\n", st
->pwr_down
);
132 static ssize_t
ad5791_write_dac_powerdown(struct device
*dev
,
133 struct device_attribute
*attr
,
134 const char *buf
, size_t len
)
138 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
139 struct ad5791_state
*st
= iio_priv(indio_dev
);
141 ret
= strict_strtol(buf
, 10, &readin
);
146 st
->pwr_down
= false;
147 st
->ctrl
&= ~(AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
148 } else if (readin
== 1) {
150 if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_6K
)
151 st
->ctrl
|= AD5791_CTRL_OPGND
;
152 else if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_3STATE
)
153 st
->ctrl
|= AD5791_CTRL_DACTRI
;
157 ret
= ad5791_spi_write(st
->spi
, AD5791_ADDR_CTRL
, st
->ctrl
);
159 return ret
? ret
: len
;
162 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode
, S_IRUGO
|
163 S_IWUSR
, ad5791_read_powerdown_mode
,
164 ad5791_write_powerdown_mode
, 0);
166 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available
,
167 "6kohm_to_gnd three_state");
169 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
170 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
171 S_IRUGO | S_IWUSR, _show, _store, _addr)
173 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5791_read_dac_powerdown
,
174 ad5791_write_dac_powerdown
, 0);
176 static struct attribute
*ad5791_attributes
[] = {
177 &iio_dev_attr_out_voltage0_powerdown
.dev_attr
.attr
,
178 &iio_dev_attr_out_voltage_powerdown_mode
.dev_attr
.attr
,
179 &iio_const_attr_out_voltage_powerdown_mode_available
.dev_attr
.attr
,
183 static const struct attribute_group ad5791_attribute_group
= {
184 .attrs
= ad5791_attributes
,
187 static int ad5791_get_lin_comp(unsigned int span
)
190 return AD5791_LINCOMP_0_10
;
191 else if (span
<= 12000)
192 return AD5791_LINCOMP_10_12
;
193 else if (span
<= 16000)
194 return AD5791_LINCOMP_12_16
;
195 else if (span
<= 19000)
196 return AD5791_LINCOMP_16_19
;
198 return AD5791_LINCOMP_19_20
;
201 static int ad5780_get_lin_comp(unsigned int span
)
204 return AD5780_LINCOMP_0_10
;
206 return AD5780_LINCOMP_10_20
;
208 static const struct ad5791_chip_info ad5791_chip_info_tbl
[] = {
210 .get_lin_comp
= ad5780_get_lin_comp
,
213 .get_lin_comp
= ad5780_get_lin_comp
,
216 .get_lin_comp
= ad5791_get_lin_comp
,
219 .get_lin_comp
= ad5791_get_lin_comp
,
223 static int ad5791_read_raw(struct iio_dev
*indio_dev
,
224 struct iio_chan_spec
const *chan
,
229 struct ad5791_state
*st
= iio_priv(indio_dev
);
235 ret
= ad5791_spi_read(st
->spi
, chan
->address
, val
);
238 *val
&= AD5791_DAC_MASK
;
239 *val
>>= chan
->scan_type
.shift
;
241 case IIO_CHAN_INFO_SCALE
:
243 *val2
= (((u64
)st
->vref_mv
) * 1000000ULL) >> chan
->scan_type
.realbits
;
244 return IIO_VAL_INT_PLUS_MICRO
;
245 case IIO_CHAN_INFO_OFFSET
:
246 val64
= (((u64
)st
->vref_neg_mv
) << chan
->scan_type
.realbits
);
247 do_div(val64
, st
->vref_mv
);
257 static int ad5791_write_raw(struct iio_dev
*indio_dev
,
258 struct iio_chan_spec
const *chan
,
263 struct ad5791_state
*st
= iio_priv(indio_dev
);
267 val
&= AD5791_RES_MASK(chan
->scan_type
.realbits
);
268 val
<<= chan
->scan_type
.shift
;
270 return ad5791_spi_write(st
->spi
, chan
->address
, val
);
277 static const struct iio_info ad5791_info
= {
278 .read_raw
= &ad5791_read_raw
,
279 .write_raw
= &ad5791_write_raw
,
280 .attrs
= &ad5791_attribute_group
,
281 .driver_module
= THIS_MODULE
,
284 static int __devinit
ad5791_probe(struct spi_device
*spi
)
286 struct ad5791_platform_data
*pdata
= spi
->dev
.platform_data
;
287 struct iio_dev
*indio_dev
;
288 struct ad5791_state
*st
;
289 int ret
, pos_voltage_uv
= 0, neg_voltage_uv
= 0;
291 indio_dev
= iio_allocate_device(sizeof(*st
));
292 if (indio_dev
== NULL
) {
296 st
= iio_priv(indio_dev
);
297 st
->reg_vdd
= regulator_get(&spi
->dev
, "vdd");
298 if (!IS_ERR(st
->reg_vdd
)) {
299 ret
= regulator_enable(st
->reg_vdd
);
301 goto error_put_reg_pos
;
303 pos_voltage_uv
= regulator_get_voltage(st
->reg_vdd
);
306 st
->reg_vss
= regulator_get(&spi
->dev
, "vss");
307 if (!IS_ERR(st
->reg_vss
)) {
308 ret
= regulator_enable(st
->reg_vss
);
310 goto error_put_reg_neg
;
312 neg_voltage_uv
= regulator_get_voltage(st
->reg_vss
);
318 if (!IS_ERR(st
->reg_vss
) && !IS_ERR(st
->reg_vdd
)) {
319 st
->vref_mv
= (pos_voltage_uv
+ neg_voltage_uv
) / 1000;
320 st
->vref_neg_mv
= neg_voltage_uv
/ 1000;
322 st
->vref_mv
= pdata
->vref_pos_mv
+ pdata
->vref_neg_mv
;
323 st
->vref_neg_mv
= pdata
->vref_neg_mv
;
325 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
328 ret
= ad5791_spi_write(spi
, AD5791_ADDR_SW_CTRL
, AD5791_SWCTRL_RESET
);
330 goto error_disable_reg_neg
;
332 st
->chip_info
= &ad5791_chip_info_tbl
[spi_get_device_id(spi
)
336 st
->ctrl
= AD5761_CTRL_LINCOMP(st
->chip_info
->get_lin_comp(st
->vref_mv
))
337 | ((pdata
&& pdata
->use_rbuf_gain2
) ? 0 : AD5791_CTRL_RBUF
) |
340 ret
= ad5791_spi_write(spi
, AD5791_ADDR_CTRL
, st
->ctrl
|
341 AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
343 goto error_disable_reg_neg
;
345 spi_set_drvdata(spi
, indio_dev
);
346 indio_dev
->dev
.parent
= &spi
->dev
;
347 indio_dev
->info
= &ad5791_info
;
348 indio_dev
->modes
= INDIO_DIRECT_MODE
;
350 = &ad5791_channels
[spi_get_device_id(spi
)->driver_data
];
351 indio_dev
->num_channels
= 1;
352 indio_dev
->name
= spi_get_device_id(st
->spi
)->name
;
353 ret
= iio_device_register(indio_dev
);
355 goto error_disable_reg_neg
;
359 error_disable_reg_neg
:
360 if (!IS_ERR(st
->reg_vss
))
361 regulator_disable(st
->reg_vss
);
363 if (!IS_ERR(st
->reg_vss
))
364 regulator_put(st
->reg_vss
);
366 if (!IS_ERR(st
->reg_vdd
))
367 regulator_disable(st
->reg_vdd
);
369 if (!IS_ERR(st
->reg_vdd
))
370 regulator_put(st
->reg_vdd
);
371 iio_free_device(indio_dev
);
377 static int __devexit
ad5791_remove(struct spi_device
*spi
)
379 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
380 struct ad5791_state
*st
= iio_priv(indio_dev
);
382 iio_device_unregister(indio_dev
);
383 if (!IS_ERR(st
->reg_vdd
)) {
384 regulator_disable(st
->reg_vdd
);
385 regulator_put(st
->reg_vdd
);
388 if (!IS_ERR(st
->reg_vss
)) {
389 regulator_disable(st
->reg_vss
);
390 regulator_put(st
->reg_vss
);
392 iio_free_device(indio_dev
);
397 static const struct spi_device_id ad5791_id
[] = {
398 {"ad5760", ID_AD5760
},
399 {"ad5780", ID_AD5780
},
400 {"ad5781", ID_AD5781
},
401 {"ad5790", ID_AD5791
},
402 {"ad5791", ID_AD5791
},
405 MODULE_DEVICE_TABLE(spi
, ad5791_id
);
407 static struct spi_driver ad5791_driver
= {
410 .owner
= THIS_MODULE
,
412 .probe
= ad5791_probe
,
413 .remove
= __devexit_p(ad5791_remove
),
414 .id_table
= ad5791_id
,
416 module_spi_driver(ad5791_driver
);
418 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
419 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
420 MODULE_LICENSE("GPL v2");