2 * Analog devices AD5764, AD5764R, AD5744, AD5744R quad-channel
3 * 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>
22 #define AD5764_REG_SF_NOP 0x0
23 #define AD5764_REG_SF_CONFIG 0x1
24 #define AD5764_REG_SF_CLEAR 0x4
25 #define AD5764_REG_SF_LOAD 0x5
26 #define AD5764_REG_DATA(x) ((2 << 3) | (x))
27 #define AD5764_REG_COARSE_GAIN(x) ((3 << 3) | (x))
28 #define AD5764_REG_FINE_GAIN(x) ((4 << 3) | (x))
29 #define AD5764_REG_OFFSET(x) ((5 << 3) | (x))
31 #define AD5764_NUM_CHANNELS 4
34 * struct ad5764_chip_info - chip specific information
35 * @int_vref: Value of the internal reference voltage in uV - 0 if external
36 * reference voltage is used
37 * @channel channel specification
40 struct ad5764_chip_info {
41 unsigned long int_vref;
42 const struct iio_chan_spec *channels;
46 * struct ad5764_state - driver instance specific data
48 * @chip_info: chip info
49 * @vref_reg: vref supply regulators
50 * @data: spi transfer buffers
54 struct spi_device *spi;
55 const struct ad5764_chip_info *chip_info;
56 struct regulator_bulk_data vref_reg[2];
59 * DMA (thus cache coherency maintenance) requires the
60 * transfer buffers to live in their own cache lines.
65 } data[2] ____cacheline_aligned;
75 #define AD5764_CHANNEL(_chan, _bits) { \
76 .type = IIO_VOLTAGE, \
81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
82 BIT(IIO_CHAN_INFO_SCALE) | \
83 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
84 BIT(IIO_CHAN_INFO_CALIBBIAS), \
85 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET), \
88 .realbits = (_bits), \
90 .shift = 16 - (_bits), \
94 #define DECLARE_AD5764_CHANNELS(_name, _bits) \
95 const struct iio_chan_spec _name##_channels[] = { \
96 AD5764_CHANNEL(0, (_bits)), \
97 AD5764_CHANNEL(1, (_bits)), \
98 AD5764_CHANNEL(2, (_bits)), \
99 AD5764_CHANNEL(3, (_bits)), \
102 static DECLARE_AD5764_CHANNELS(ad5764, 16);
103 static DECLARE_AD5764_CHANNELS(ad5744, 14);
105 static const struct ad5764_chip_info ad5764_chip_infos[] = {
108 .channels = ad5744_channels,
112 .channels = ad5744_channels,
116 .channels = ad5764_channels,
120 .channels = ad5764_channels,
124 static int ad5764_write(struct iio_dev *indio_dev, unsigned int reg,
127 struct ad5764_state *st = iio_priv(indio_dev);
130 mutex_lock(&indio_dev->mlock);
131 st->data[0].d32 = cpu_to_be32((reg << 16) | val);
133 ret = spi_write(st->spi, &st->data[0].d8[1], 3);
134 mutex_unlock(&indio_dev->mlock);
139 static int ad5764_read(struct iio_dev *indio_dev, unsigned int reg,
142 struct ad5764_state *st = iio_priv(indio_dev);
144 struct spi_transfer t[] = {
146 .tx_buf = &st->data[0].d8[1],
150 .rx_buf = &st->data[1].d8[1],
155 mutex_lock(&indio_dev->mlock);
157 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
159 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
161 *val = be32_to_cpu(st->data[1].d32) & 0xffff;
163 mutex_unlock(&indio_dev->mlock);
168 static int ad5764_chan_info_to_reg(struct iio_chan_spec const *chan, long info)
171 case IIO_CHAN_INFO_RAW:
172 return AD5764_REG_DATA(chan->address);
173 case IIO_CHAN_INFO_CALIBBIAS:
174 return AD5764_REG_OFFSET(chan->address);
175 case IIO_CHAN_INFO_CALIBSCALE:
176 return AD5764_REG_FINE_GAIN(chan->address);
184 static int ad5764_write_raw(struct iio_dev *indio_dev,
185 struct iio_chan_spec const *chan, int val, int val2, long info)
187 const int max_val = (1 << chan->scan_type.realbits);
191 case IIO_CHAN_INFO_RAW:
192 if (val >= max_val || val < 0)
194 val <<= chan->scan_type.shift;
196 case IIO_CHAN_INFO_CALIBBIAS:
197 if (val >= 128 || val < -128)
200 case IIO_CHAN_INFO_CALIBSCALE:
201 if (val >= 32 || val < -32)
208 reg = ad5764_chan_info_to_reg(chan, info);
209 return ad5764_write(indio_dev, reg, (u16)val);
212 static int ad5764_get_channel_vref(struct ad5764_state *st,
213 unsigned int channel)
215 if (st->chip_info->int_vref)
216 return st->chip_info->int_vref;
218 return regulator_get_voltage(st->vref_reg[channel / 2].consumer);
221 static int ad5764_read_raw(struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan, int *val, int *val2, long info)
224 struct ad5764_state *st = iio_priv(indio_dev);
230 case IIO_CHAN_INFO_RAW:
231 reg = AD5764_REG_DATA(chan->address);
232 ret = ad5764_read(indio_dev, reg, val);
235 *val >>= chan->scan_type.shift;
237 case IIO_CHAN_INFO_CALIBBIAS:
238 reg = AD5764_REG_OFFSET(chan->address);
239 ret = ad5764_read(indio_dev, reg, val);
242 *val = sign_extend32(*val, 7);
244 case IIO_CHAN_INFO_CALIBSCALE:
245 reg = AD5764_REG_FINE_GAIN(chan->address);
246 ret = ad5764_read(indio_dev, reg, val);
249 *val = sign_extend32(*val, 5);
251 case IIO_CHAN_INFO_SCALE:
252 /* vout = 4 * vref + ((dac_code / 65536) - 0.5) */
253 vref = ad5764_get_channel_vref(st, chan->channel);
257 *val = vref * 4 / 1000;
258 *val2 = chan->scan_type.realbits;
259 return IIO_VAL_FRACTIONAL_LOG2;
260 case IIO_CHAN_INFO_OFFSET:
261 *val = -(1 << chan->scan_type.realbits) / 2;
268 static const struct iio_info ad5764_info = {
269 .read_raw = ad5764_read_raw,
270 .write_raw = ad5764_write_raw,
273 static int ad5764_probe(struct spi_device *spi)
275 enum ad5764_type type = spi_get_device_id(spi)->driver_data;
276 struct iio_dev *indio_dev;
277 struct ad5764_state *st;
280 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
281 if (indio_dev == NULL) {
282 dev_err(&spi->dev, "Failed to allocate iio device\n");
286 st = iio_priv(indio_dev);
287 spi_set_drvdata(spi, indio_dev);
290 st->chip_info = &ad5764_chip_infos[type];
292 indio_dev->dev.parent = &spi->dev;
293 indio_dev->name = spi_get_device_id(spi)->name;
294 indio_dev->info = &ad5764_info;
295 indio_dev->modes = INDIO_DIRECT_MODE;
296 indio_dev->num_channels = AD5764_NUM_CHANNELS;
297 indio_dev->channels = st->chip_info->channels;
299 if (st->chip_info->int_vref == 0) {
300 st->vref_reg[0].supply = "vrefAB";
301 st->vref_reg[1].supply = "vrefCD";
303 ret = devm_regulator_bulk_get(&st->spi->dev,
304 ARRAY_SIZE(st->vref_reg), st->vref_reg);
306 dev_err(&spi->dev, "Failed to request vref regulators: %d\n",
311 ret = regulator_bulk_enable(ARRAY_SIZE(st->vref_reg),
314 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n",
320 ret = iio_device_register(indio_dev);
322 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
323 goto error_disable_reg;
329 if (st->chip_info->int_vref == 0)
330 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
334 static int ad5764_remove(struct spi_device *spi)
336 struct iio_dev *indio_dev = spi_get_drvdata(spi);
337 struct ad5764_state *st = iio_priv(indio_dev);
339 iio_device_unregister(indio_dev);
341 if (st->chip_info->int_vref == 0)
342 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
347 static const struct spi_device_id ad5764_ids[] = {
348 { "ad5744", ID_AD5744 },
349 { "ad5744r", ID_AD5744R },
350 { "ad5764", ID_AD5764 },
351 { "ad5764r", ID_AD5764R },
354 MODULE_DEVICE_TABLE(spi, ad5764_ids);
356 static struct spi_driver ad5764_driver = {
360 .probe = ad5764_probe,
361 .remove = ad5764_remove,
362 .id_table = ad5764_ids,
364 module_spi_driver(ad5764_driver);
366 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
367 MODULE_DESCRIPTION("Analog Devices AD5744/AD5744R/AD5764/AD5764R DAC");
368 MODULE_LICENSE("GPL v2");