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>
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 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 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 const struct iio_chan_spec *chan, const char *buf, size_t len)
165 struct ad5064_state *st = iio_priv(indio_dev);
166 unsigned int mode, i;
171 for (i = 1; i < ARRAY_SIZE(ad5064_powerdown_modes); ++i) {
172 if (sysfs_streq(buf, ad5064_powerdown_modes[i])) {
180 mutex_lock(&indio_dev->mlock);
181 st->pwr_down_mode[chan->channel] = mode;
183 ret = ad5064_sync_powerdown_mode(st, chan->channel);
184 mutex_unlock(&indio_dev->mlock);
186 return ret ? ret : len;
189 static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
190 const struct iio_chan_spec *chan, char *buf)
192 struct ad5064_state *st = iio_priv(indio_dev);
194 return sprintf(buf, "%d\n", st->pwr_down[chan->channel]);
197 static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
198 const struct iio_chan_spec *chan, const char *buf, size_t len)
200 struct ad5064_state *st = iio_priv(indio_dev);
204 ret = strtobool(buf, &pwr_down);
208 mutex_lock(&indio_dev->mlock);
209 st->pwr_down[chan->channel] = pwr_down;
211 ret = ad5064_sync_powerdown_mode(st, chan->channel);
212 mutex_unlock(&indio_dev->mlock);
213 return ret ? ret : len;
216 static int ad5064_get_vref(struct ad5064_state *st,
217 struct iio_chan_spec const *chan)
221 if (st->use_internal_vref)
222 return st->chip_info->internal_vref;
224 i = st->chip_info->shared_vref ? 0 : chan->channel;
225 return regulator_get_voltage(st->vref_reg[i].consumer);
228 static int ad5064_read_raw(struct iio_dev *indio_dev,
229 struct iio_chan_spec const *chan,
234 struct ad5064_state *st = iio_priv(indio_dev);
239 *val = st->dac_cache[chan->channel];
241 case IIO_CHAN_INFO_SCALE:
242 scale_uv = ad5064_get_vref(st, chan);
246 scale_uv = (scale_uv * 100) >> chan->scan_type.realbits;
247 *val = scale_uv / 100000;
248 *val2 = (scale_uv % 100000) * 10;
249 return IIO_VAL_INT_PLUS_MICRO;
256 static int ad5064_write_raw(struct iio_dev *indio_dev,
257 struct iio_chan_spec const *chan, int val, int val2, long mask)
259 struct ad5064_state *st = iio_priv(indio_dev);
264 if (val > (1 << chan->scan_type.realbits) || val < 0)
267 mutex_lock(&indio_dev->mlock);
268 ret = ad5064_spi_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
269 chan->address, val, chan->scan_type.shift);
271 st->dac_cache[chan->channel] = val;
272 mutex_unlock(&indio_dev->mlock);
281 static const struct iio_info ad5064_info = {
282 .read_raw = ad5064_read_raw,
283 .write_raw = ad5064_write_raw,
284 .driver_module = THIS_MODULE,
287 static struct iio_chan_spec_ext_info ad5064_ext_info[] = {
290 .read = ad5064_read_dac_powerdown,
291 .write = ad5064_write_dac_powerdown,
294 .name = "powerdown_mode",
295 .read = ad5064_read_powerdown_mode,
296 .write = ad5064_write_powerdown_mode,
299 .name = "powerdown_mode_available",
301 .read = ad5064_read_powerdown_mode_available,
306 #define AD5064_CHANNEL(chan, bits) { \
307 .type = IIO_VOLTAGE, \
311 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
312 .address = AD5064_ADDR_DAC(chan), \
313 .scan_type = IIO_ST('u', (bits), 16, 20 - (bits)), \
314 .ext_info = ad5064_ext_info, \
317 #define DECLARE_AD5064_CHANNELS(name, bits) \
318 const struct iio_chan_spec name[] = { \
319 AD5064_CHANNEL(0, bits), \
320 AD5064_CHANNEL(1, bits), \
321 AD5064_CHANNEL(2, bits), \
322 AD5064_CHANNEL(3, bits), \
323 AD5064_CHANNEL(4, bits), \
324 AD5064_CHANNEL(5, bits), \
325 AD5064_CHANNEL(6, bits), \
326 AD5064_CHANNEL(7, bits), \
329 static DECLARE_AD5064_CHANNELS(ad5024_channels, 12);
330 static DECLARE_AD5064_CHANNELS(ad5044_channels, 14);
331 static DECLARE_AD5064_CHANNELS(ad5064_channels, 16);
333 static const struct ad5064_chip_info ad5064_chip_info_tbl[] = {
335 .shared_vref = false,
336 .channels = ad5024_channels,
340 .shared_vref = false,
341 .channels = ad5024_channels,
345 .shared_vref = false,
346 .channels = ad5044_channels,
350 .shared_vref = false,
351 .channels = ad5044_channels,
355 .shared_vref = false,
356 .channels = ad5064_channels,
361 .channels = ad5064_channels,
365 .shared_vref = false,
366 .channels = ad5064_channels,
371 .internal_vref = 2500000,
372 .channels = ad5024_channels,
377 .internal_vref = 5000000,
378 .channels = ad5024_channels,
383 .internal_vref = 2500000,
384 .channels = ad5044_channels,
389 .internal_vref = 5000000,
390 .channels = ad5044_channels,
395 .internal_vref = 2500000,
396 .channels = ad5064_channels,
401 .internal_vref = 5000000,
402 .channels = ad5064_channels,
407 .internal_vref = 2500000,
408 .channels = ad5064_channels,
413 .internal_vref = 5000000,
414 .channels = ad5064_channels,
419 static inline unsigned int ad5064_num_vref(struct ad5064_state *st)
421 return st->chip_info->shared_vref ? 1 : st->chip_info->num_channels;
424 static const char * const ad5064_vref_names[] = {
431 static const char * const ad5064_vref_name(struct ad5064_state *st,
434 return st->chip_info->shared_vref ? "vref" : ad5064_vref_names[vref];
437 static int __devinit ad5064_probe(struct spi_device *spi)
439 enum ad5064_type type = spi_get_device_id(spi)->driver_data;
440 struct iio_dev *indio_dev;
441 struct ad5064_state *st;
445 indio_dev = iio_allocate_device(sizeof(*st));
446 if (indio_dev == NULL)
449 st = iio_priv(indio_dev);
450 spi_set_drvdata(spi, indio_dev);
452 st->chip_info = &ad5064_chip_info_tbl[type];
455 for (i = 0; i < ad5064_num_vref(st); ++i)
456 st->vref_reg[i].supply = ad5064_vref_name(st, i);
458 ret = regulator_bulk_get(&st->spi->dev, ad5064_num_vref(st),
461 if (!st->chip_info->internal_vref)
463 st->use_internal_vref = true;
464 ret = ad5064_spi_write(st, AD5064_CMD_CONFIG, 0,
465 AD5064_CONFIG_INT_VREF_ENABLE, 0);
467 dev_err(&spi->dev, "Failed to enable internal vref: %d\n",
472 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
477 for (i = 0; i < st->chip_info->num_channels; ++i) {
478 st->pwr_down_mode[i] = AD5064_LDAC_PWRDN_1K;
479 st->dac_cache[i] = 0x8000;
482 indio_dev->dev.parent = &spi->dev;
483 indio_dev->name = spi_get_device_id(spi)->name;
484 indio_dev->info = &ad5064_info;
485 indio_dev->modes = INDIO_DIRECT_MODE;
486 indio_dev->channels = st->chip_info->channels;
487 indio_dev->num_channels = st->chip_info->num_channels;
489 ret = iio_device_register(indio_dev);
491 goto error_disable_reg;
496 if (!st->use_internal_vref)
497 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
499 if (!st->use_internal_vref)
500 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
502 iio_free_device(indio_dev);
508 static int __devexit ad5064_remove(struct spi_device *spi)
510 struct iio_dev *indio_dev = spi_get_drvdata(spi);
511 struct ad5064_state *st = iio_priv(indio_dev);
513 iio_device_unregister(indio_dev);
515 if (!st->use_internal_vref) {
516 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
517 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
520 iio_free_device(indio_dev);
525 static const struct spi_device_id ad5064_id[] = {
526 {"ad5024", ID_AD5024},
527 {"ad5025", ID_AD5025},
528 {"ad5044", ID_AD5044},
529 {"ad5045", ID_AD5045},
530 {"ad5064", ID_AD5064},
531 {"ad5064-1", ID_AD5064_1},
532 {"ad5065", ID_AD5065},
533 {"ad5628-1", ID_AD5628_1},
534 {"ad5628-2", ID_AD5628_2},
535 {"ad5648-1", ID_AD5648_1},
536 {"ad5648-2", ID_AD5648_2},
537 {"ad5666-1", ID_AD5666_1},
538 {"ad5666-2", ID_AD5666_2},
539 {"ad5668-1", ID_AD5668_1},
540 {"ad5668-2", ID_AD5668_2},
541 {"ad5668-3", ID_AD5668_2}, /* similar enough to ad5668-2 */
544 MODULE_DEVICE_TABLE(spi, ad5064_id);
546 static struct spi_driver ad5064_driver = {
549 .owner = THIS_MODULE,
551 .probe = ad5064_probe,
552 .remove = __devexit_p(ad5064_remove),
553 .id_table = ad5064_id,
555 module_spi_driver(ad5064_driver);
557 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
558 MODULE_DESCRIPTION("Analog Devices AD5024/25/44/45/64/64-1/65, AD5628/48/66/68 DAC");
559 MODULE_LICENSE("GPL v2");