2 * AD5024, AD5025, AD5044, AD5045, AD5064, AD5064-1, AD5065, AD5628, AD5648,
3 * AD5666, AD5668 Digital to analog converters driver
5 * Copyright 2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/device.h>
11 #include <linux/err.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>
23 #define AD5064_MAX_DAC_CHANNELS 8
24 #define AD5064_MAX_VREFS 4
26 #define AD5064_ADDR(x) ((x) << 20)
27 #define AD5064_CMD(x) ((x) << 24)
29 #define AD5064_ADDR_DAC(chan) (chan)
30 #define AD5064_ADDR_ALL_DAC 0xF
32 #define AD5064_CMD_WRITE_INPUT_N 0x0
33 #define AD5064_CMD_UPDATE_DAC_N 0x1
34 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2
35 #define AD5064_CMD_WRITE_INPUT_N_UPDATE_N 0x3
36 #define AD5064_CMD_POWERDOWN_DAC 0x4
37 #define AD5064_CMD_CLEAR 0x5
38 #define AD5064_CMD_LDAC_MASK 0x6
39 #define AD5064_CMD_RESET 0x7
40 #define AD5064_CMD_CONFIG 0x8
42 #define AD5064_CONFIG_DAISY_CHAIN_ENABLE BIT(1)
43 #define AD5064_CONFIG_INT_VREF_ENABLE BIT(0)
45 #define AD5064_LDAC_PWRDN_NONE 0x0
46 #define AD5064_LDAC_PWRDN_1K 0x1
47 #define AD5064_LDAC_PWRDN_100K 0x2
48 #define AD5064_LDAC_PWRDN_3STATE 0x3
51 * struct ad5064_chip_info - chip specific information
52 * @shared_vref: whether the vref supply is shared between channels
53 * @internal_vref: internal reference voltage. 0 if the chip has no internal
55 * @channel: channel specification
56 * @num_channels: number of channels
59 struct ad5064_chip_info {
61 unsigned long internal_vref;
62 const struct iio_chan_spec *channels;
63 unsigned int num_channels;
67 * struct ad5064_state - driver instance specific data
69 * @chip_info: chip model specific constants, available modes etc
70 * @vref_reg: vref supply regulators
71 * @pwr_down: whether channel is powered down
72 * @pwr_down_mode: channel's current power down mode
73 * @dac_cache: current DAC raw value (chip does not support readback)
74 * @use_internal_vref: set to true if the internal reference voltage should be
76 * @data: spi transfer buffers
80 struct spi_device *spi;
81 const struct ad5064_chip_info *chip_info;
82 struct regulator_bulk_data vref_reg[AD5064_MAX_VREFS];
83 bool pwr_down[AD5064_MAX_DAC_CHANNELS];
84 u8 pwr_down_mode[AD5064_MAX_DAC_CHANNELS];
85 unsigned int dac_cache[AD5064_MAX_DAC_CHANNELS];
86 bool use_internal_vref;
89 * DMA (thus cache coherency maintenance) requires the
90 * transfer buffers to live in their own cache lines.
92 __be32 data ____cacheline_aligned;
113 static int ad5064_spi_write(struct ad5064_state *st, unsigned int cmd,
114 unsigned int addr, unsigned int val, unsigned int shift)
118 st->data = cpu_to_be32(AD5064_CMD(cmd) | AD5064_ADDR(addr) | val);
120 return spi_write(st->spi, &st->data, sizeof(st->data));
123 static int ad5064_sync_powerdown_mode(struct ad5064_state *st,
124 unsigned int channel)
129 val = (0x1 << channel);
131 if (st->pwr_down[channel])
132 val |= st->pwr_down_mode[channel] << 8;
134 ret = ad5064_spi_write(st, AD5064_CMD_POWERDOWN_DAC, 0, val, 0);
139 static const char ad5064_powerdown_modes[][15] = {
140 [AD5064_LDAC_PWRDN_NONE] = "",
141 [AD5064_LDAC_PWRDN_1K] = "1kohm_to_gnd",
142 [AD5064_LDAC_PWRDN_100K] = "100kohm_to_gnd",
143 [AD5064_LDAC_PWRDN_3STATE] = "three_state",
146 static ssize_t ad5064_read_powerdown_mode_available(struct iio_dev *indio_dev,
147 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
149 return sprintf(buf, "%s %s %s\n", ad5064_powerdown_modes[1],
150 ad5064_powerdown_modes[2], ad5064_powerdown_modes[3]);
153 static ssize_t ad5064_read_powerdown_mode(struct iio_dev *indio_dev,
154 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
156 struct ad5064_state *st = iio_priv(indio_dev);
158 return sprintf(buf, "%s\n",
159 ad5064_powerdown_modes[st->pwr_down_mode[chan->channel]]);
162 static ssize_t ad5064_write_powerdown_mode(struct iio_dev *indio_dev,
163 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
166 struct ad5064_state *st = iio_priv(indio_dev);
167 unsigned int mode, i;
172 for (i = 1; i < ARRAY_SIZE(ad5064_powerdown_modes); ++i) {
173 if (sysfs_streq(buf, ad5064_powerdown_modes[i])) {
181 mutex_lock(&indio_dev->mlock);
182 st->pwr_down_mode[chan->channel] = mode;
184 ret = ad5064_sync_powerdown_mode(st, chan->channel);
185 mutex_unlock(&indio_dev->mlock);
187 return ret ? ret : len;
190 static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
191 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
193 struct ad5064_state *st = iio_priv(indio_dev);
195 return sprintf(buf, "%d\n", st->pwr_down[chan->channel]);
198 static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
199 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
202 struct ad5064_state *st = iio_priv(indio_dev);
206 ret = strtobool(buf, &pwr_down);
210 mutex_lock(&indio_dev->mlock);
211 st->pwr_down[chan->channel] = pwr_down;
213 ret = ad5064_sync_powerdown_mode(st, chan->channel);
214 mutex_unlock(&indio_dev->mlock);
215 return ret ? ret : len;
218 static int ad5064_get_vref(struct ad5064_state *st,
219 struct iio_chan_spec const *chan)
223 if (st->use_internal_vref)
224 return st->chip_info->internal_vref;
226 i = st->chip_info->shared_vref ? 0 : chan->channel;
227 return regulator_get_voltage(st->vref_reg[i].consumer);
230 static int ad5064_read_raw(struct iio_dev *indio_dev,
231 struct iio_chan_spec const *chan,
236 struct ad5064_state *st = iio_priv(indio_dev);
240 case IIO_CHAN_INFO_RAW:
241 *val = st->dac_cache[chan->channel];
243 case IIO_CHAN_INFO_SCALE:
244 scale_uv = ad5064_get_vref(st, chan);
248 scale_uv = (scale_uv * 100) >> chan->scan_type.realbits;
249 *val = scale_uv / 100000;
250 *val2 = (scale_uv % 100000) * 10;
251 return IIO_VAL_INT_PLUS_MICRO;
258 static int ad5064_write_raw(struct iio_dev *indio_dev,
259 struct iio_chan_spec const *chan, int val, int val2, long mask)
261 struct ad5064_state *st = iio_priv(indio_dev);
265 case IIO_CHAN_INFO_RAW:
266 if (val > (1 << chan->scan_type.realbits) || val < 0)
269 mutex_lock(&indio_dev->mlock);
270 ret = ad5064_spi_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
271 chan->address, val, chan->scan_type.shift);
273 st->dac_cache[chan->channel] = val;
274 mutex_unlock(&indio_dev->mlock);
283 static const struct iio_info ad5064_info = {
284 .read_raw = ad5064_read_raw,
285 .write_raw = ad5064_write_raw,
286 .driver_module = THIS_MODULE,
289 static struct iio_chan_spec_ext_info ad5064_ext_info[] = {
292 .read = ad5064_read_dac_powerdown,
293 .write = ad5064_write_dac_powerdown,
296 .name = "powerdown_mode",
297 .read = ad5064_read_powerdown_mode,
298 .write = ad5064_write_powerdown_mode,
301 .name = "powerdown_mode_available",
303 .read = ad5064_read_powerdown_mode_available,
308 #define AD5064_CHANNEL(chan, bits) { \
309 .type = IIO_VOLTAGE, \
313 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
314 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
315 .address = AD5064_ADDR_DAC(chan), \
316 .scan_type = IIO_ST('u', (bits), 16, 20 - (bits)), \
317 .ext_info = ad5064_ext_info, \
320 #define DECLARE_AD5064_CHANNELS(name, bits) \
321 const struct iio_chan_spec name[] = { \
322 AD5064_CHANNEL(0, bits), \
323 AD5064_CHANNEL(1, bits), \
324 AD5064_CHANNEL(2, bits), \
325 AD5064_CHANNEL(3, bits), \
326 AD5064_CHANNEL(4, bits), \
327 AD5064_CHANNEL(5, bits), \
328 AD5064_CHANNEL(6, bits), \
329 AD5064_CHANNEL(7, bits), \
332 static DECLARE_AD5064_CHANNELS(ad5024_channels, 12);
333 static DECLARE_AD5064_CHANNELS(ad5044_channels, 14);
334 static DECLARE_AD5064_CHANNELS(ad5064_channels, 16);
336 static const struct ad5064_chip_info ad5064_chip_info_tbl[] = {
338 .shared_vref = false,
339 .channels = ad5024_channels,
343 .shared_vref = false,
344 .channels = ad5024_channels,
348 .shared_vref = false,
349 .channels = ad5044_channels,
353 .shared_vref = false,
354 .channels = ad5044_channels,
358 .shared_vref = false,
359 .channels = ad5064_channels,
364 .channels = ad5064_channels,
368 .shared_vref = false,
369 .channels = ad5064_channels,
374 .internal_vref = 2500000,
375 .channels = ad5024_channels,
380 .internal_vref = 5000000,
381 .channels = ad5024_channels,
386 .internal_vref = 2500000,
387 .channels = ad5044_channels,
392 .internal_vref = 5000000,
393 .channels = ad5044_channels,
398 .internal_vref = 2500000,
399 .channels = ad5064_channels,
404 .internal_vref = 5000000,
405 .channels = ad5064_channels,
410 .internal_vref = 2500000,
411 .channels = ad5064_channels,
416 .internal_vref = 5000000,
417 .channels = ad5064_channels,
422 static inline unsigned int ad5064_num_vref(struct ad5064_state *st)
424 return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels;
427 static const char * const ad5064_vref_names[] = {
434 static const char * const ad5064_vref_name(struct ad5064_state *st,
437 return st->chip_info->shared_vref ? "vref" : ad5064_vref_names[vref];
440 static int __devinit ad5064_probe(struct spi_device *spi)
442 enum ad5064_type type = spi_get_device_id(spi)->driver_data;
443 struct iio_dev *indio_dev;
444 struct ad5064_state *st;
448 indio_dev = iio_device_alloc(sizeof(*st));
449 if (indio_dev == NULL)
452 st = iio_priv(indio_dev);
453 spi_set_drvdata(spi, indio_dev);
455 st->chip_info = &ad5064_chip_info_tbl[type];
458 for (i = 0; i < ad5064_num_vref(st); ++i)
459 st->vref_reg[i].supply = ad5064_vref_name(st, i);
461 ret = regulator_bulk_get(&st->spi->dev, ad5064_num_vref(st),
464 if (!st->chip_info->internal_vref)
466 st->use_internal_vref = true;
467 ret = ad5064_spi_write(st, AD5064_CMD_CONFIG, 0,
468 AD5064_CONFIG_INT_VREF_ENABLE, 0);
470 dev_err(&spi->dev, "Failed to enable internal vref: %d\n",
475 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
480 for (i = 0; i < st->chip_info->num_channels; ++i) {
481 st->pwr_down_mode[i] = AD5064_LDAC_PWRDN_1K;
482 st->dac_cache[i] = 0x8000;
485 indio_dev->dev.parent = &spi->dev;
486 indio_dev->name = spi_get_device_id(spi)->name;
487 indio_dev->info = &ad5064_info;
488 indio_dev->modes = INDIO_DIRECT_MODE;
489 indio_dev->channels = st->chip_info->channels;
490 indio_dev->num_channels = st->chip_info->num_channels;
492 ret = iio_device_register(indio_dev);
494 goto error_disable_reg;
499 if (!st->use_internal_vref)
500 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
502 if (!st->use_internal_vref)
503 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
505 iio_device_free(indio_dev);
511 static int __devexit ad5064_remove(struct spi_device *spi)
513 struct iio_dev *indio_dev = spi_get_drvdata(spi);
514 struct ad5064_state *st = iio_priv(indio_dev);
516 iio_device_unregister(indio_dev);
518 if (!st->use_internal_vref) {
519 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
520 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
523 iio_device_free(indio_dev);
528 static const struct spi_device_id ad5064_id[] = {
529 {"ad5024", ID_AD5024},
530 {"ad5025", ID_AD5025},
531 {"ad5044", ID_AD5044},
532 {"ad5045", ID_AD5045},
533 {"ad5064", ID_AD5064},
534 {"ad5064-1", ID_AD5064_1},
535 {"ad5065", ID_AD5065},
536 {"ad5628-1", ID_AD5628_1},
537 {"ad5628-2", ID_AD5628_2},
538 {"ad5648-1", ID_AD5648_1},
539 {"ad5648-2", ID_AD5648_2},
540 {"ad5666-1", ID_AD5666_1},
541 {"ad5666-2", ID_AD5666_2},
542 {"ad5668-1", ID_AD5668_1},
543 {"ad5668-2", ID_AD5668_2},
544 {"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */
547 MODULE_DEVICE_TABLE(spi, ad5064_id);
549 static struct spi_driver ad5064_driver = {
552 .owner = THIS_MODULE,
554 .probe = ad5064_probe,
555 .remove = __devexit_p(ad5064_remove),
556 .id_table = ad5064_id,
558 module_spi_driver(ad5064_driver);
560 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
561 MODULE_DESCRIPTION("Analog Devices AD5024/25/44/45/64/64-1/65, AD5628/48/66/68 DAC");
562 MODULE_LICENSE("GPL v2");