2 * AD5686R, AD5685R, AD5684R Digital to analog converters driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/regulator/consumer.h>
24 #define AD5686_DAC_CHANNELS 4
26 #define AD5686_ADDR(x) ((x) << 16)
27 #define AD5686_CMD(x) ((x) << 20)
29 #define AD5686_ADDR_DAC0 0x1
30 #define AD5686_ADDR_DAC1 0x2
31 #define AD5686_ADDR_DAC2 0x4
32 #define AD5686_ADDR_DAC3 0x8
33 #define AD5686_ADDR_ALL_DAC 0xF
35 #define AD5686_CMD_NOOP 0x0
36 #define AD5686_CMD_WRITE_INPUT_N 0x1
37 #define AD5686_CMD_UPDATE_DAC_N 0x2
38 #define AD5686_CMD_WRITE_INPUT_N_UPDATE_N 0x3
39 #define AD5686_CMD_POWERDOWN_DAC 0x4
40 #define AD5686_CMD_LDAC_MASK 0x5
41 #define AD5686_CMD_RESET 0x6
42 #define AD5686_CMD_INTERNAL_REFER_SETUP 0x7
43 #define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8
44 #define AD5686_CMD_READBACK_ENABLE 0x9
46 #define AD5686_LDAC_PWRDN_NONE 0x0
47 #define AD5686_LDAC_PWRDN_1K 0x1
48 #define AD5686_LDAC_PWRDN_100K 0x2
49 #define AD5686_LDAC_PWRDN_3STATE 0x3
52 * struct ad5686_chip_info - chip specific information
53 * @int_vref_mv: AD5620/40/60: the internal reference voltage
54 * @channel: channel specification
57 struct ad5686_chip_info
{
59 struct iio_chan_spec channel
[AD5686_DAC_CHANNELS
];
63 * struct ad5446_state - driver instance specific data
65 * @chip_info: chip model specific constants, available modes etc
66 * @reg: supply regulator
67 * @vref_mv: actual reference voltage used
68 * @pwr_down_mask: power down mask
69 * @pwr_down_mode: current power down mode
70 * @data: spi transfer buffers
74 struct spi_device
*spi
;
75 const struct ad5686_chip_info
*chip_info
;
76 struct regulator
*reg
;
77 unsigned short vref_mv
;
78 unsigned pwr_down_mask
;
79 unsigned pwr_down_mode
;
81 * DMA (thus cache coherency maintenance) requires the
82 * transfer buffers to live in their own cache lines.
88 } data
[3] ____cacheline_aligned
;
92 * ad5686_supported_device_ids:
95 enum ad5686_supported_device_ids
{
100 #define AD5868_CHANNEL(chan, bits, shift) { \
101 .type = IIO_VOLTAGE, \
105 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
106 .address = AD5686_ADDR_DAC0, \
107 .scan_type = IIO_ST('u', bits, 16, shift) \
109 static const struct ad5686_chip_info ad5686_chip_info_tbl
[] = {
111 .channel
[0] = AD5868_CHANNEL(0, 12, 4),
112 .channel
[1] = AD5868_CHANNEL(1, 12, 4),
113 .channel
[2] = AD5868_CHANNEL(2, 12, 4),
114 .channel
[3] = AD5868_CHANNEL(3, 12, 4),
118 .channel
[0] = AD5868_CHANNEL(0, 14, 2),
119 .channel
[1] = AD5868_CHANNEL(1, 14, 2),
120 .channel
[2] = AD5868_CHANNEL(2, 14, 2),
121 .channel
[3] = AD5868_CHANNEL(3, 14, 2),
125 .channel
[0] = AD5868_CHANNEL(0, 16, 0),
126 .channel
[1] = AD5868_CHANNEL(1, 16, 0),
127 .channel
[2] = AD5868_CHANNEL(2, 16, 0),
128 .channel
[3] = AD5868_CHANNEL(3, 16, 0),
133 static int ad5686_spi_write(struct ad5686_state
*st
,
134 u8 cmd
, u8 addr
, u16 val
, u8 shift
)
138 st
->data
[0].d32
= cpu_to_be32(AD5686_CMD(cmd
) |
142 return spi_write(st
->spi
, &st
->data
[0].d8
[1], 3);
145 static int ad5686_spi_read(struct ad5686_state
*st
, u8 addr
)
147 struct spi_transfer t
[] = {
149 .tx_buf
= &st
->data
[0].d8
[1],
153 .tx_buf
= &st
->data
[1].d8
[1],
154 .rx_buf
= &st
->data
[2].d8
[1],
158 struct spi_message m
;
161 spi_message_init(&m
);
162 spi_message_add_tail(&t
[0], &m
);
163 spi_message_add_tail(&t
[1], &m
);
165 st
->data
[0].d32
= cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE
) |
167 st
->data
[1].d32
= cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP
));
169 ret
= spi_sync(st
->spi
, &m
);
173 return be32_to_cpu(st
->data
[2].d32
);
176 static ssize_t
ad5686_read_powerdown_mode(struct device
*dev
,
177 struct device_attribute
*attr
, char *buf
)
179 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
180 struct ad5686_state
*st
= iio_priv(indio_dev
);
181 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
183 char mode
[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
185 return sprintf(buf
, "%s\n", mode
[(st
->pwr_down_mode
>>
186 (this_attr
->address
* 2)) & 0x3]);
189 static ssize_t
ad5686_write_powerdown_mode(struct device
*dev
,
190 struct device_attribute
*attr
,
191 const char *buf
, size_t len
)
193 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
194 struct ad5686_state
*st
= iio_priv(indio_dev
);
195 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
198 if (sysfs_streq(buf
, "1kohm_to_gnd"))
199 mode
= AD5686_LDAC_PWRDN_1K
;
200 else if (sysfs_streq(buf
, "100kohm_to_gnd"))
201 mode
= AD5686_LDAC_PWRDN_100K
;
202 else if (sysfs_streq(buf
, "three_state"))
203 mode
= AD5686_LDAC_PWRDN_3STATE
;
207 st
->pwr_down_mode
&= ~(0x3 << (this_attr
->address
* 2));
208 st
->pwr_down_mode
|= (mode
<< (this_attr
->address
* 2));
213 static ssize_t
ad5686_read_dac_powerdown(struct device
*dev
,
214 struct device_attribute
*attr
,
217 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
218 struct ad5686_state
*st
= iio_priv(indio_dev
);
219 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
221 return sprintf(buf
, "%d\n", !!(st
->pwr_down_mask
&
222 (0x3 << (this_attr
->address
* 2))));
225 static ssize_t
ad5686_write_dac_powerdown(struct device
*dev
,
226 struct device_attribute
*attr
,
227 const char *buf
, size_t len
)
231 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
232 struct ad5686_state
*st
= iio_priv(indio_dev
);
233 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
235 ret
= strtobool(buf
, &readin
);
240 st
->pwr_down_mask
|= (0x3 << (this_attr
->address
* 2));
242 st
->pwr_down_mask
&= ~(0x3 << (this_attr
->address
* 2));
244 ret
= ad5686_spi_write(st
, AD5686_CMD_POWERDOWN_DAC
, 0,
245 st
->pwr_down_mask
& st
->pwr_down_mode
, 0);
247 return ret
? ret
: len
;
250 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available
,
251 "1kohm_to_gnd 100kohm_to_gnd three_state");
253 #define IIO_DEV_ATTR_DAC_POWERDOWN_MODE(_num) \
254 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown_mode, \
256 ad5686_read_powerdown_mode, \
257 ad5686_write_powerdown_mode, _num)
259 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(0);
260 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(1);
261 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(2);
262 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(3);
264 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num) \
265 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
267 ad5686_read_dac_powerdown, \
268 ad5686_write_dac_powerdown, _num)
270 static IIO_DEV_ATTR_DAC_POWERDOWN(0);
271 static IIO_DEV_ATTR_DAC_POWERDOWN(1);
272 static IIO_DEV_ATTR_DAC_POWERDOWN(2);
273 static IIO_DEV_ATTR_DAC_POWERDOWN(3);
275 static struct attribute
*ad5686_attributes
[] = {
276 &iio_dev_attr_out_voltage0_powerdown
.dev_attr
.attr
,
277 &iio_dev_attr_out_voltage1_powerdown
.dev_attr
.attr
,
278 &iio_dev_attr_out_voltage2_powerdown
.dev_attr
.attr
,
279 &iio_dev_attr_out_voltage3_powerdown
.dev_attr
.attr
,
280 &iio_dev_attr_out_voltage0_powerdown_mode
.dev_attr
.attr
,
281 &iio_dev_attr_out_voltage1_powerdown_mode
.dev_attr
.attr
,
282 &iio_dev_attr_out_voltage2_powerdown_mode
.dev_attr
.attr
,
283 &iio_dev_attr_out_voltage3_powerdown_mode
.dev_attr
.attr
,
284 &iio_const_attr_out_voltage_powerdown_mode_available
.dev_attr
.attr
,
288 static const struct attribute_group ad5686_attribute_group
= {
289 .attrs
= ad5686_attributes
,
292 static int ad5686_read_raw(struct iio_dev
*indio_dev
,
293 struct iio_chan_spec
const *chan
,
298 struct ad5686_state
*st
= iio_priv(indio_dev
);
299 unsigned long scale_uv
;
304 mutex_lock(&indio_dev
->mlock
);
305 ret
= ad5686_spi_read(st
, chan
->address
);
306 mutex_unlock(&indio_dev
->mlock
);
312 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
313 scale_uv
= (st
->vref_mv
* 100000)
314 >> (chan
->scan_type
.realbits
);
315 *val
= scale_uv
/ 100000;
316 *val2
= (scale_uv
% 100000) * 10;
317 return IIO_VAL_INT_PLUS_MICRO
;
323 static int ad5686_write_raw(struct iio_dev
*indio_dev
,
324 struct iio_chan_spec
const *chan
,
329 struct ad5686_state
*st
= iio_priv(indio_dev
);
334 if (val
> (1 << chan
->scan_type
.realbits
))
337 mutex_lock(&indio_dev
->mlock
);
338 ret
= ad5686_spi_write(st
,
339 AD5686_CMD_WRITE_INPUT_N_UPDATE_N
,
342 chan
->scan_type
.shift
);
343 mutex_unlock(&indio_dev
->mlock
);
352 static const struct iio_info ad5686_info
= {
353 .read_raw
= ad5686_read_raw
,
354 .write_raw
= ad5686_write_raw
,
355 .attrs
= &ad5686_attribute_group
,
356 .driver_module
= THIS_MODULE
,
359 static int __devinit
ad5686_probe(struct spi_device
*spi
)
361 struct ad5686_state
*st
;
362 struct iio_dev
*indio_dev
;
363 int ret
, regdone
= 0, voltage_uv
= 0;
365 indio_dev
= iio_allocate_device(sizeof(*st
));
366 if (indio_dev
== NULL
)
369 st
= iio_priv(indio_dev
);
370 spi_set_drvdata(spi
, indio_dev
);
372 st
->reg
= regulator_get(&spi
->dev
, "vcc");
373 if (!IS_ERR(st
->reg
)) {
374 ret
= regulator_enable(st
->reg
);
378 voltage_uv
= regulator_get_voltage(st
->reg
);
382 &ad5686_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
385 st
->vref_mv
= voltage_uv
/ 1000;
387 st
->vref_mv
= st
->chip_info
->int_vref_mv
;
391 indio_dev
->dev
.parent
= &spi
->dev
;
392 indio_dev
->name
= spi_get_device_id(spi
)->name
;
393 indio_dev
->info
= &ad5686_info
;
394 indio_dev
->modes
= INDIO_DIRECT_MODE
;
395 indio_dev
->channels
= st
->chip_info
->channel
;
396 indio_dev
->num_channels
= AD5686_DAC_CHANNELS
;
399 ret
= ad5686_spi_write(st
, AD5686_CMD_INTERNAL_REFER_SETUP
, 0,
402 goto error_disable_reg
;
404 ret
= iio_device_register(indio_dev
);
406 goto error_disable_reg
;
411 if (!IS_ERR(st
->reg
))
412 regulator_disable(st
->reg
);
414 if (!IS_ERR(st
->reg
))
415 regulator_put(st
->reg
);
417 iio_free_device(indio_dev
);
422 static int __devexit
ad5686_remove(struct spi_device
*spi
)
424 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
425 struct ad5686_state
*st
= iio_priv(indio_dev
);
427 if (!IS_ERR(st
->reg
)) {
428 regulator_disable(st
->reg
);
429 regulator_put(st
->reg
);
432 iio_device_unregister(indio_dev
);
437 static const struct spi_device_id ad5686_id
[] = {
438 {"ad5684", ID_AD5684
},
439 {"ad5685", ID_AD5685
},
440 {"ad5686", ID_AD5686
},
444 static struct spi_driver ad5686_driver
= {
447 .owner
= THIS_MODULE
,
449 .probe
= ad5686_probe
,
450 .remove
= __devexit_p(ad5686_remove
),
451 .id_table
= ad5686_id
,
454 static __init
int ad5686_spi_init(void)
456 return spi_register_driver(&ad5686_driver
);
458 module_init(ad5686_spi_init
);
460 static __exit
void ad5686_spi_exit(void)
462 spi_unregister_driver(&ad5686_driver
);
464 module_exit(ad5686_spi_exit
);
466 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
467 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
468 MODULE_LICENSE("GPL v2");