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>
23 #define AD5686_DAC_CHANNELS 4
25 #define AD5686_ADDR(x) ((x) << 16)
26 #define AD5686_CMD(x) ((x) << 20)
28 #define AD5686_ADDR_DAC(chan) (0x1 << (chan))
29 #define AD5686_ADDR_ALL_DAC 0xF
31 #define AD5686_CMD_NOOP 0x0
32 #define AD5686_CMD_WRITE_INPUT_N 0x1
33 #define AD5686_CMD_UPDATE_DAC_N 0x2
34 #define AD5686_CMD_WRITE_INPUT_N_UPDATE_N 0x3
35 #define AD5686_CMD_POWERDOWN_DAC 0x4
36 #define AD5686_CMD_LDAC_MASK 0x5
37 #define AD5686_CMD_RESET 0x6
38 #define AD5686_CMD_INTERNAL_REFER_SETUP 0x7
39 #define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8
40 #define AD5686_CMD_READBACK_ENABLE 0x9
42 #define AD5686_LDAC_PWRDN_NONE 0x0
43 #define AD5686_LDAC_PWRDN_1K 0x1
44 #define AD5686_LDAC_PWRDN_100K 0x2
45 #define AD5686_LDAC_PWRDN_3STATE 0x3
48 * struct ad5686_chip_info - chip specific information
49 * @int_vref_mv: AD5620/40/60: the internal reference voltage
50 * @channel: channel specification
53 struct ad5686_chip_info {
55 struct iio_chan_spec channel[AD5686_DAC_CHANNELS];
59 * struct ad5446_state - driver instance specific data
61 * @chip_info: chip model specific constants, available modes etc
62 * @reg: supply regulator
63 * @vref_mv: actual reference voltage used
64 * @pwr_down_mask: power down mask
65 * @pwr_down_mode: current power down mode
66 * @data: spi transfer buffers
70 struct spi_device *spi;
71 const struct ad5686_chip_info *chip_info;
72 struct regulator *reg;
73 unsigned short vref_mv;
74 unsigned pwr_down_mask;
75 unsigned pwr_down_mode;
77 * DMA (thus cache coherency maintenance) requires the
78 * transfer buffers to live in their own cache lines.
84 } data[3] ____cacheline_aligned;
88 * ad5686_supported_device_ids:
91 enum ad5686_supported_device_ids {
96 #define AD5868_CHANNEL(chan, bits, shift) { \
97 .type = IIO_VOLTAGE, \
101 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
102 .address = AD5686_ADDR_DAC(chan), \
103 .scan_type = IIO_ST('u', bits, 16, shift) \
105 static const struct ad5686_chip_info ad5686_chip_info_tbl[] = {
107 .channel[0] = AD5868_CHANNEL(0, 12, 4),
108 .channel[1] = AD5868_CHANNEL(1, 12, 4),
109 .channel[2] = AD5868_CHANNEL(2, 12, 4),
110 .channel[3] = AD5868_CHANNEL(3, 12, 4),
114 .channel[0] = AD5868_CHANNEL(0, 14, 2),
115 .channel[1] = AD5868_CHANNEL(1, 14, 2),
116 .channel[2] = AD5868_CHANNEL(2, 14, 2),
117 .channel[3] = AD5868_CHANNEL(3, 14, 2),
121 .channel[0] = AD5868_CHANNEL(0, 16, 0),
122 .channel[1] = AD5868_CHANNEL(1, 16, 0),
123 .channel[2] = AD5868_CHANNEL(2, 16, 0),
124 .channel[3] = AD5868_CHANNEL(3, 16, 0),
129 static int ad5686_spi_write(struct ad5686_state *st,
130 u8 cmd, u8 addr, u16 val, u8 shift)
134 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) |
138 return spi_write(st->spi, &st->data[0].d8[1], 3);
141 static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
143 struct spi_transfer t[] = {
145 .tx_buf = &st->data[0].d8[1],
149 .tx_buf = &st->data[1].d8[1],
150 .rx_buf = &st->data[2].d8[1],
154 struct spi_message m;
157 spi_message_init(&m);
158 spi_message_add_tail(&t[0], &m);
159 spi_message_add_tail(&t[1], &m);
161 st->data[0].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE) |
163 st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP));
165 ret = spi_sync(st->spi, &m);
169 return be32_to_cpu(st->data[2].d32);
172 static ssize_t ad5686_read_powerdown_mode(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct iio_dev *indio_dev = dev_get_drvdata(dev);
176 struct ad5686_state *st = iio_priv(indio_dev);
177 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
181 return sprintf(buf, "%s\n", mode[(st->pwr_down_mode >>
182 (this_attr->address * 2)) & 0x3]);
185 static ssize_t ad5686_write_powerdown_mode(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t len)
189 struct iio_dev *indio_dev = dev_get_drvdata(dev);
190 struct ad5686_state *st = iio_priv(indio_dev);
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
194 if (sysfs_streq(buf, "1kohm_to_gnd"))
195 mode = AD5686_LDAC_PWRDN_1K;
196 else if (sysfs_streq(buf, "100kohm_to_gnd"))
197 mode = AD5686_LDAC_PWRDN_100K;
198 else if (sysfs_streq(buf, "three_state"))
199 mode = AD5686_LDAC_PWRDN_3STATE;
203 st->pwr_down_mode &= ~(0x3 << (this_attr->address * 2));
204 st->pwr_down_mode |= (mode << (this_attr->address * 2));
209 static ssize_t ad5686_read_dac_powerdown(struct device *dev,
210 struct device_attribute *attr,
213 struct iio_dev *indio_dev = dev_get_drvdata(dev);
214 struct ad5686_state *st = iio_priv(indio_dev);
215 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
217 return sprintf(buf, "%d\n", !!(st->pwr_down_mask &
218 (0x3 << (this_attr->address * 2))));
221 static ssize_t ad5686_write_dac_powerdown(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf, size_t len)
227 struct iio_dev *indio_dev = dev_get_drvdata(dev);
228 struct ad5686_state *st = iio_priv(indio_dev);
229 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
231 ret = strtobool(buf, &readin);
236 st->pwr_down_mask |= (0x3 << (this_attr->address * 2));
238 st->pwr_down_mask &= ~(0x3 << (this_attr->address * 2));
240 ret = ad5686_spi_write(st, AD5686_CMD_POWERDOWN_DAC, 0,
241 st->pwr_down_mask & st->pwr_down_mode, 0);
243 return ret ? ret : len;
246 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
247 "1kohm_to_gnd 100kohm_to_gnd three_state");
249 #define IIO_DEV_ATTR_DAC_POWERDOWN_MODE(_num) \
250 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown_mode, \
252 ad5686_read_powerdown_mode, \
253 ad5686_write_powerdown_mode, _num)
255 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(0);
256 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(1);
257 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(2);
258 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(3);
260 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num) \
261 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
263 ad5686_read_dac_powerdown, \
264 ad5686_write_dac_powerdown, _num)
266 static IIO_DEV_ATTR_DAC_POWERDOWN(0);
267 static IIO_DEV_ATTR_DAC_POWERDOWN(1);
268 static IIO_DEV_ATTR_DAC_POWERDOWN(2);
269 static IIO_DEV_ATTR_DAC_POWERDOWN(3);
271 static struct attribute *ad5686_attributes[] = {
272 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
273 &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr,
274 &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr,
275 &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr,
276 &iio_dev_attr_out_voltage0_powerdown_mode.dev_attr.attr,
277 &iio_dev_attr_out_voltage1_powerdown_mode.dev_attr.attr,
278 &iio_dev_attr_out_voltage2_powerdown_mode.dev_attr.attr,
279 &iio_dev_attr_out_voltage3_powerdown_mode.dev_attr.attr,
280 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
284 static const struct attribute_group ad5686_attribute_group = {
285 .attrs = ad5686_attributes,
288 static int ad5686_read_raw(struct iio_dev *indio_dev,
289 struct iio_chan_spec const *chan,
294 struct ad5686_state *st = iio_priv(indio_dev);
295 unsigned long scale_uv;
300 mutex_lock(&indio_dev->mlock);
301 ret = ad5686_spi_read(st, chan->address);
302 mutex_unlock(&indio_dev->mlock);
308 case IIO_CHAN_INFO_SCALE:
309 scale_uv = (st->vref_mv * 100000)
310 >> (chan->scan_type.realbits);
311 *val = scale_uv / 100000;
312 *val2 = (scale_uv % 100000) * 10;
313 return IIO_VAL_INT_PLUS_MICRO;
319 static int ad5686_write_raw(struct iio_dev *indio_dev,
320 struct iio_chan_spec const *chan,
325 struct ad5686_state *st = iio_priv(indio_dev);
330 if (val > (1 << chan->scan_type.realbits) || val < 0)
333 mutex_lock(&indio_dev->mlock);
334 ret = ad5686_spi_write(st,
335 AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
338 chan->scan_type.shift);
339 mutex_unlock(&indio_dev->mlock);
348 static const struct iio_info ad5686_info = {
349 .read_raw = ad5686_read_raw,
350 .write_raw = ad5686_write_raw,
351 .attrs = &ad5686_attribute_group,
352 .driver_module = THIS_MODULE,
355 static int __devinit ad5686_probe(struct spi_device *spi)
357 struct ad5686_state *st;
358 struct iio_dev *indio_dev;
359 int ret, regdone = 0, voltage_uv = 0;
361 indio_dev = iio_allocate_device(sizeof(*st));
362 if (indio_dev == NULL)
365 st = iio_priv(indio_dev);
366 spi_set_drvdata(spi, indio_dev);
368 st->reg = regulator_get(&spi->dev, "vcc");
369 if (!IS_ERR(st->reg)) {
370 ret = regulator_enable(st->reg);
374 voltage_uv = regulator_get_voltage(st->reg);
378 &ad5686_chip_info_tbl[spi_get_device_id(spi)->driver_data];
381 st->vref_mv = voltage_uv / 1000;
383 st->vref_mv = st->chip_info->int_vref_mv;
387 indio_dev->dev.parent = &spi->dev;
388 indio_dev->name = spi_get_device_id(spi)->name;
389 indio_dev->info = &ad5686_info;
390 indio_dev->modes = INDIO_DIRECT_MODE;
391 indio_dev->channels = st->chip_info->channel;
392 indio_dev->num_channels = AD5686_DAC_CHANNELS;
395 ret = ad5686_spi_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
398 goto error_disable_reg;
400 ret = iio_device_register(indio_dev);
402 goto error_disable_reg;
407 if (!IS_ERR(st->reg))
408 regulator_disable(st->reg);
410 if (!IS_ERR(st->reg))
411 regulator_put(st->reg);
413 iio_free_device(indio_dev);
418 static int __devexit ad5686_remove(struct spi_device *spi)
420 struct iio_dev *indio_dev = spi_get_drvdata(spi);
421 struct ad5686_state *st = iio_priv(indio_dev);
423 iio_device_unregister(indio_dev);
424 if (!IS_ERR(st->reg)) {
425 regulator_disable(st->reg);
426 regulator_put(st->reg);
428 iio_free_device(indio_dev);
433 static const struct spi_device_id ad5686_id[] = {
434 {"ad5684", ID_AD5684},
435 {"ad5685", ID_AD5685},
436 {"ad5686", ID_AD5686},
439 MODULE_DEVICE_TABLE(spi, ad5686_id);
441 static struct spi_driver ad5686_driver = {
444 .owner = THIS_MODULE,
446 .probe = ad5686_probe,
447 .remove = __devexit_p(ad5686_remove),
448 .id_table = ad5686_id,
450 module_spi_driver(ad5686_driver);
452 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
453 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
454 MODULE_LICENSE("GPL v2");