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>
11 #include <linux/device.h>
12 #include <linux/module.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>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #define AD5686_DAC_CHANNELS 4
24 #define AD5686_ADDR(x) ((x) << 16)
25 #define AD5686_CMD(x) ((x) << 20)
27 #define AD5686_ADDR_DAC(chan) (0x1 << (chan))
28 #define AD5686_ADDR_ALL_DAC 0xF
30 #define AD5686_CMD_NOOP 0x0
31 #define AD5686_CMD_WRITE_INPUT_N 0x1
32 #define AD5686_CMD_UPDATE_DAC_N 0x2
33 #define AD5686_CMD_WRITE_INPUT_N_UPDATE_N 0x3
34 #define AD5686_CMD_POWERDOWN_DAC 0x4
35 #define AD5686_CMD_LDAC_MASK 0x5
36 #define AD5686_CMD_RESET 0x6
37 #define AD5686_CMD_INTERNAL_REFER_SETUP 0x7
38 #define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8
39 #define AD5686_CMD_READBACK_ENABLE 0x9
41 #define AD5686_LDAC_PWRDN_NONE 0x0
42 #define AD5686_LDAC_PWRDN_1K 0x1
43 #define AD5686_LDAC_PWRDN_100K 0x2
44 #define AD5686_LDAC_PWRDN_3STATE 0x3
47 * struct ad5686_chip_info - chip specific information
48 * @int_vref_mv: AD5620/40/60: the internal reference voltage
49 * @channel: channel specification
52 struct ad5686_chip_info
{
54 struct iio_chan_spec channel
[AD5686_DAC_CHANNELS
];
58 * struct ad5446_state - driver instance specific data
60 * @chip_info: chip model specific constants, available modes etc
61 * @reg: supply regulator
62 * @vref_mv: actual reference voltage used
63 * @pwr_down_mask: power down mask
64 * @pwr_down_mode: current power down mode
65 * @data: spi transfer buffers
69 struct spi_device
*spi
;
70 const struct ad5686_chip_info
*chip_info
;
71 struct regulator
*reg
;
72 unsigned short vref_mv
;
73 unsigned pwr_down_mask
;
74 unsigned pwr_down_mode
;
76 * DMA (thus cache coherency maintenance) requires the
77 * transfer buffers to live in their own cache lines.
83 } data
[3] ____cacheline_aligned
;
87 * ad5686_supported_device_ids:
90 enum ad5686_supported_device_ids
{
95 static int ad5686_spi_write(struct ad5686_state
*st
,
96 u8 cmd
, u8 addr
, u16 val
, u8 shift
)
100 st
->data
[0].d32
= cpu_to_be32(AD5686_CMD(cmd
) |
104 return spi_write(st
->spi
, &st
->data
[0].d8
[1], 3);
107 static int ad5686_spi_read(struct ad5686_state
*st
, u8 addr
)
109 struct spi_transfer t
[] = {
111 .tx_buf
= &st
->data
[0].d8
[1],
115 .tx_buf
= &st
->data
[1].d8
[1],
116 .rx_buf
= &st
->data
[2].d8
[1],
122 st
->data
[0].d32
= cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE
) |
124 st
->data
[1].d32
= cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP
));
126 ret
= spi_sync_transfer(st
->spi
, t
, ARRAY_SIZE(t
));
130 return be32_to_cpu(st
->data
[2].d32
);
133 static const char * const ad5686_powerdown_modes
[] = {
139 static int ad5686_get_powerdown_mode(struct iio_dev
*indio_dev
,
140 const struct iio_chan_spec
*chan
)
142 struct ad5686_state
*st
= iio_priv(indio_dev
);
144 return ((st
->pwr_down_mode
>> (chan
->channel
* 2)) & 0x3) - 1;
147 static int ad5686_set_powerdown_mode(struct iio_dev
*indio_dev
,
148 const struct iio_chan_spec
*chan
, unsigned int mode
)
150 struct ad5686_state
*st
= iio_priv(indio_dev
);
152 st
->pwr_down_mode
&= ~(0x3 << (chan
->channel
* 2));
153 st
->pwr_down_mode
|= ((mode
+ 1) << (chan
->channel
* 2));
158 static const struct iio_enum ad5686_powerdown_mode_enum
= {
159 .items
= ad5686_powerdown_modes
,
160 .num_items
= ARRAY_SIZE(ad5686_powerdown_modes
),
161 .get
= ad5686_get_powerdown_mode
,
162 .set
= ad5686_set_powerdown_mode
,
165 static ssize_t
ad5686_read_dac_powerdown(struct iio_dev
*indio_dev
,
166 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
168 struct ad5686_state
*st
= iio_priv(indio_dev
);
170 return sprintf(buf
, "%d\n", !!(st
->pwr_down_mask
&
171 (0x3 << (chan
->channel
* 2))));
174 static ssize_t
ad5686_write_dac_powerdown(struct iio_dev
*indio_dev
,
175 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
180 struct ad5686_state
*st
= iio_priv(indio_dev
);
182 ret
= strtobool(buf
, &readin
);
187 st
->pwr_down_mask
|= (0x3 << (chan
->channel
* 2));
189 st
->pwr_down_mask
&= ~(0x3 << (chan
->channel
* 2));
191 ret
= ad5686_spi_write(st
, AD5686_CMD_POWERDOWN_DAC
, 0,
192 st
->pwr_down_mask
& st
->pwr_down_mode
, 0);
194 return ret
? ret
: len
;
197 static int ad5686_read_raw(struct iio_dev
*indio_dev
,
198 struct iio_chan_spec
const *chan
,
203 struct ad5686_state
*st
= iio_priv(indio_dev
);
207 case IIO_CHAN_INFO_RAW
:
208 mutex_lock(&indio_dev
->mlock
);
209 ret
= ad5686_spi_read(st
, chan
->address
);
210 mutex_unlock(&indio_dev
->mlock
);
215 case IIO_CHAN_INFO_SCALE
:
217 *val2
= chan
->scan_type
.realbits
;
218 return IIO_VAL_FRACTIONAL_LOG2
;
223 static int ad5686_write_raw(struct iio_dev
*indio_dev
,
224 struct iio_chan_spec
const *chan
,
229 struct ad5686_state
*st
= iio_priv(indio_dev
);
233 case IIO_CHAN_INFO_RAW
:
234 if (val
> (1 << chan
->scan_type
.realbits
) || val
< 0)
237 mutex_lock(&indio_dev
->mlock
);
238 ret
= ad5686_spi_write(st
,
239 AD5686_CMD_WRITE_INPUT_N_UPDATE_N
,
242 chan
->scan_type
.shift
);
243 mutex_unlock(&indio_dev
->mlock
);
252 static const struct iio_info ad5686_info
= {
253 .read_raw
= ad5686_read_raw
,
254 .write_raw
= ad5686_write_raw
,
255 .driver_module
= THIS_MODULE
,
258 static const struct iio_chan_spec_ext_info ad5686_ext_info
[] = {
261 .read
= ad5686_read_dac_powerdown
,
262 .write
= ad5686_write_dac_powerdown
,
263 .shared
= IIO_SEPARATE
,
265 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &ad5686_powerdown_mode_enum
),
266 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5686_powerdown_mode_enum
),
270 #define AD5868_CHANNEL(chan, bits, _shift) { \
271 .type = IIO_VOLTAGE, \
275 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
276 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
277 .address = AD5686_ADDR_DAC(chan), \
280 .realbits = (bits), \
284 .ext_info = ad5686_ext_info, \
287 static const struct ad5686_chip_info ad5686_chip_info_tbl
[] = {
289 .channel
[0] = AD5868_CHANNEL(0, 12, 4),
290 .channel
[1] = AD5868_CHANNEL(1, 12, 4),
291 .channel
[2] = AD5868_CHANNEL(2, 12, 4),
292 .channel
[3] = AD5868_CHANNEL(3, 12, 4),
296 .channel
[0] = AD5868_CHANNEL(0, 14, 2),
297 .channel
[1] = AD5868_CHANNEL(1, 14, 2),
298 .channel
[2] = AD5868_CHANNEL(2, 14, 2),
299 .channel
[3] = AD5868_CHANNEL(3, 14, 2),
303 .channel
[0] = AD5868_CHANNEL(0, 16, 0),
304 .channel
[1] = AD5868_CHANNEL(1, 16, 0),
305 .channel
[2] = AD5868_CHANNEL(2, 16, 0),
306 .channel
[3] = AD5868_CHANNEL(3, 16, 0),
312 static int ad5686_probe(struct spi_device
*spi
)
314 struct ad5686_state
*st
;
315 struct iio_dev
*indio_dev
;
316 int ret
, regdone
= 0, voltage_uv
= 0;
318 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
319 if (indio_dev
== NULL
)
322 st
= iio_priv(indio_dev
);
323 spi_set_drvdata(spi
, indio_dev
);
325 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
326 if (!IS_ERR(st
->reg
)) {
327 ret
= regulator_enable(st
->reg
);
331 ret
= regulator_get_voltage(st
->reg
);
333 goto error_disable_reg
;
339 &ad5686_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
342 st
->vref_mv
= voltage_uv
/ 1000;
344 st
->vref_mv
= st
->chip_info
->int_vref_mv
;
348 /* Set all the power down mode for all channels to 1K pulldown */
349 st
->pwr_down_mode
= 0x55;
351 indio_dev
->dev
.parent
= &spi
->dev
;
352 indio_dev
->name
= spi_get_device_id(spi
)->name
;
353 indio_dev
->info
= &ad5686_info
;
354 indio_dev
->modes
= INDIO_DIRECT_MODE
;
355 indio_dev
->channels
= st
->chip_info
->channel
;
356 indio_dev
->num_channels
= AD5686_DAC_CHANNELS
;
359 ret
= ad5686_spi_write(st
, AD5686_CMD_INTERNAL_REFER_SETUP
, 0,
362 goto error_disable_reg
;
364 ret
= iio_device_register(indio_dev
);
366 goto error_disable_reg
;
371 if (!IS_ERR(st
->reg
))
372 regulator_disable(st
->reg
);
376 static int ad5686_remove(struct spi_device
*spi
)
378 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
379 struct ad5686_state
*st
= iio_priv(indio_dev
);
381 iio_device_unregister(indio_dev
);
382 if (!IS_ERR(st
->reg
))
383 regulator_disable(st
->reg
);
388 static const struct spi_device_id ad5686_id
[] = {
389 {"ad5684", ID_AD5684
},
390 {"ad5685", ID_AD5685
},
391 {"ad5686", ID_AD5686
},
394 MODULE_DEVICE_TABLE(spi
, ad5686_id
);
396 static struct spi_driver ad5686_driver
= {
399 .owner
= THIS_MODULE
,
401 .probe
= ad5686_probe
,
402 .remove
= ad5686_remove
,
403 .id_table
= ad5686_id
,
405 module_spi_driver(ad5686_driver
);
407 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
408 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
409 MODULE_LICENSE("GPL v2");