1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog devices AD5360, AD5361, AD5362, AD5363, AD5370, AD5371, AD5373
4 * multi-channel Digital to Analog Converters driver
6 * Copyright 2011 Analog Devices Inc.
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/regulator/consumer.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define AD5360_CMD(x) ((x) << 22)
22 #define AD5360_ADDR(x) ((x) << 16)
24 #define AD5360_READBACK_TYPE(x) ((x) << 13)
25 #define AD5360_READBACK_ADDR(x) ((x) << 7)
27 #define AD5360_CHAN_ADDR(chan) ((chan) + 0x8)
29 #define AD5360_CMD_WRITE_DATA 0x3
30 #define AD5360_CMD_WRITE_OFFSET 0x2
31 #define AD5360_CMD_WRITE_GAIN 0x1
32 #define AD5360_CMD_SPECIAL_FUNCTION 0x0
34 /* Special function register addresses */
35 #define AD5360_REG_SF_NOP 0x0
36 #define AD5360_REG_SF_CTRL 0x1
37 #define AD5360_REG_SF_OFS(x) (0x2 + (x))
38 #define AD5360_REG_SF_READBACK 0x5
40 #define AD5360_SF_CTRL_PWR_DOWN BIT(0)
42 #define AD5360_READBACK_X1A 0x0
43 #define AD5360_READBACK_X1B 0x1
44 #define AD5360_READBACK_OFFSET 0x2
45 #define AD5360_READBACK_GAIN 0x3
46 #define AD5360_READBACK_SF 0x4
50 * struct ad5360_chip_info - chip specific information
51 * @channel_template: channel specification template
52 * @num_channels: number of channels
53 * @channels_per_group: number of channels per group
54 * @num_vrefs: number of vref supplies for the chip
57 struct ad5360_chip_info {
58 struct iio_chan_spec channel_template;
59 unsigned int num_channels;
60 unsigned int channels_per_group;
61 unsigned int num_vrefs;
65 * struct ad5360_state - driver instance specific data
67 * @chip_info: chip model specific constants, available modes etc
68 * @vref_reg: vref supply regulators
69 * @ctrl: control register cache
70 * @lock: lock to protect the data buffer during SPI ops
71 * @data: spi transfer buffers
75 struct spi_device *spi;
76 const struct ad5360_chip_info *chip_info;
77 struct regulator_bulk_data vref_reg[3];
82 * DMA (thus cache coherency maintenance) may require the
83 * transfer buffers to live in their own cache lines.
88 } data[2] __aligned(IIO_DMA_MINALIGN);
102 #define AD5360_CHANNEL(bits) { \
103 .type = IIO_VOLTAGE, \
106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
107 BIT(IIO_CHAN_INFO_SCALE) | \
108 BIT(IIO_CHAN_INFO_OFFSET) | \
109 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
110 BIT(IIO_CHAN_INFO_CALIBBIAS), \
113 .realbits = (bits), \
115 .shift = 16 - (bits), \
119 static const struct ad5360_chip_info ad5360_chip_info_tbl[] = {
121 .channel_template = AD5360_CHANNEL(16),
123 .channels_per_group = 8,
127 .channel_template = AD5360_CHANNEL(14),
129 .channels_per_group = 8,
133 .channel_template = AD5360_CHANNEL(16),
135 .channels_per_group = 4,
139 .channel_template = AD5360_CHANNEL(14),
141 .channels_per_group = 4,
145 .channel_template = AD5360_CHANNEL(16),
147 .channels_per_group = 8,
151 .channel_template = AD5360_CHANNEL(14),
153 .channels_per_group = 8,
157 .channel_template = AD5360_CHANNEL(16),
159 .channels_per_group = 8,
163 .channel_template = AD5360_CHANNEL(14),
165 .channels_per_group = 8,
170 static unsigned int ad5360_get_channel_vref_index(struct ad5360_state *st,
171 unsigned int channel)
175 /* The first groups have their own vref, while the remaining groups
176 * share the last vref */
177 i = channel / st->chip_info->channels_per_group;
178 if (i >= st->chip_info->num_vrefs)
179 i = st->chip_info->num_vrefs - 1;
184 static int ad5360_get_channel_vref(struct ad5360_state *st,
185 unsigned int channel)
187 unsigned int i = ad5360_get_channel_vref_index(st, channel);
189 return regulator_get_voltage(st->vref_reg[i].consumer);
193 static int ad5360_write_unlocked(struct iio_dev *indio_dev,
194 unsigned int cmd, unsigned int addr, unsigned int val,
197 struct ad5360_state *st = iio_priv(indio_dev);
200 val |= AD5360_CMD(cmd) | AD5360_ADDR(addr);
201 st->data[0].d32 = cpu_to_be32(val);
203 return spi_write(st->spi, &st->data[0].d8[1], 3);
206 static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
207 unsigned int addr, unsigned int val, unsigned int shift)
210 struct ad5360_state *st = iio_priv(indio_dev);
212 mutex_lock(&st->lock);
213 ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
214 mutex_unlock(&st->lock);
219 static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
222 struct ad5360_state *st = iio_priv(indio_dev);
224 struct spi_transfer t[] = {
226 .tx_buf = &st->data[0].d8[1],
230 .rx_buf = &st->data[1].d8[1],
235 mutex_lock(&st->lock);
237 st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
238 AD5360_ADDR(AD5360_REG_SF_READBACK) |
239 AD5360_READBACK_TYPE(type) |
240 AD5360_READBACK_ADDR(addr));
242 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
244 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
246 mutex_unlock(&st->lock);
251 static ssize_t ad5360_read_dac_powerdown(struct device *dev,
252 struct device_attribute *attr,
255 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
256 struct ad5360_state *st = iio_priv(indio_dev);
258 return sysfs_emit(buf, "%d\n", (bool)(st->ctrl & AD5360_SF_CTRL_PWR_DOWN));
261 static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
264 struct ad5360_state *st = iio_priv(indio_dev);
267 mutex_lock(&st->lock);
272 ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
273 AD5360_REG_SF_CTRL, st->ctrl, 0);
275 mutex_unlock(&st->lock);
280 static ssize_t ad5360_write_dac_powerdown(struct device *dev,
281 struct device_attribute *attr, const char *buf, size_t len)
283 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
287 ret = kstrtobool(buf, &pwr_down);
292 ret = ad5360_update_ctrl(indio_dev, AD5360_SF_CTRL_PWR_DOWN, 0);
294 ret = ad5360_update_ctrl(indio_dev, 0, AD5360_SF_CTRL_PWR_DOWN);
296 return ret ? ret : len;
299 static IIO_DEVICE_ATTR(out_voltage_powerdown,
301 ad5360_read_dac_powerdown,
302 ad5360_write_dac_powerdown, 0);
304 static struct attribute *ad5360_attributes[] = {
305 &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
309 static const struct attribute_group ad5360_attribute_group = {
310 .attrs = ad5360_attributes,
313 static int ad5360_write_raw(struct iio_dev *indio_dev,
314 struct iio_chan_spec const *chan,
319 struct ad5360_state *st = iio_priv(indio_dev);
320 int max_val = (1 << chan->scan_type.realbits);
321 unsigned int ofs_index;
324 case IIO_CHAN_INFO_RAW:
325 if (val >= max_val || val < 0)
328 return ad5360_write(indio_dev, AD5360_CMD_WRITE_DATA,
329 chan->address, val, chan->scan_type.shift);
331 case IIO_CHAN_INFO_CALIBBIAS:
332 if (val >= max_val || val < 0)
335 return ad5360_write(indio_dev, AD5360_CMD_WRITE_OFFSET,
336 chan->address, val, chan->scan_type.shift);
338 case IIO_CHAN_INFO_CALIBSCALE:
339 if (val >= max_val || val < 0)
342 return ad5360_write(indio_dev, AD5360_CMD_WRITE_GAIN,
343 chan->address, val, chan->scan_type.shift);
345 case IIO_CHAN_INFO_OFFSET:
346 if (val <= -max_val || val > 0)
351 /* offset is supposed to have the same scale as raw, but it
352 * is always 14bits wide, so on a chip where the raw value has
353 * more bits, we need to shift offset. */
354 val >>= (chan->scan_type.realbits - 14);
356 /* There is one DAC offset register per vref. Changing one
357 * channels offset will also change the offset for all other
358 * channels which share the same vref supply. */
359 ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
360 return ad5360_write(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
361 AD5360_REG_SF_OFS(ofs_index), val, 0);
369 static int ad5360_read_raw(struct iio_dev *indio_dev,
370 struct iio_chan_spec const *chan,
375 struct ad5360_state *st = iio_priv(indio_dev);
376 unsigned int ofs_index;
381 case IIO_CHAN_INFO_RAW:
382 ret = ad5360_read(indio_dev, AD5360_READBACK_X1A,
386 *val = ret >> chan->scan_type.shift;
388 case IIO_CHAN_INFO_SCALE:
389 scale_uv = ad5360_get_channel_vref(st, chan->channel);
393 /* vout = 4 * vref * dac_code */
394 *val = scale_uv * 4 / 1000;
395 *val2 = chan->scan_type.realbits;
396 return IIO_VAL_FRACTIONAL_LOG2;
397 case IIO_CHAN_INFO_CALIBBIAS:
398 ret = ad5360_read(indio_dev, AD5360_READBACK_OFFSET,
404 case IIO_CHAN_INFO_CALIBSCALE:
405 ret = ad5360_read(indio_dev, AD5360_READBACK_GAIN,
411 case IIO_CHAN_INFO_OFFSET:
412 ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
413 ret = ad5360_read(indio_dev, AD5360_READBACK_SF,
414 AD5360_REG_SF_OFS(ofs_index));
418 ret <<= (chan->scan_type.realbits - 14);
426 static const struct iio_info ad5360_info = {
427 .read_raw = ad5360_read_raw,
428 .write_raw = ad5360_write_raw,
429 .attrs = &ad5360_attribute_group,
432 static const char * const ad5360_vref_name[] = {
433 "vref0", "vref1", "vref2"
436 static int ad5360_alloc_channels(struct iio_dev *indio_dev)
438 struct ad5360_state *st = iio_priv(indio_dev);
439 struct iio_chan_spec *channels;
442 channels = kcalloc(st->chip_info->num_channels,
443 sizeof(struct iio_chan_spec), GFP_KERNEL);
448 for (i = 0; i < st->chip_info->num_channels; ++i) {
449 channels[i] = st->chip_info->channel_template;
450 channels[i].channel = i;
451 channels[i].address = AD5360_CHAN_ADDR(i);
454 indio_dev->channels = channels;
459 static int ad5360_probe(struct spi_device *spi)
461 enum ad5360_type type = spi_get_device_id(spi)->driver_data;
462 struct iio_dev *indio_dev;
463 struct ad5360_state *st;
467 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
468 if (indio_dev == NULL) {
469 dev_err(&spi->dev, "Failed to allocate iio device\n");
473 st = iio_priv(indio_dev);
474 spi_set_drvdata(spi, indio_dev);
476 st->chip_info = &ad5360_chip_info_tbl[type];
479 indio_dev->name = spi_get_device_id(spi)->name;
480 indio_dev->info = &ad5360_info;
481 indio_dev->modes = INDIO_DIRECT_MODE;
482 indio_dev->num_channels = st->chip_info->num_channels;
484 mutex_init(&st->lock);
486 ret = ad5360_alloc_channels(indio_dev);
488 dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret);
492 for (i = 0; i < st->chip_info->num_vrefs; ++i)
493 st->vref_reg[i].supply = ad5360_vref_name[i];
495 ret = devm_regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs,
498 dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret);
499 goto error_free_channels;
502 ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg);
504 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret);
505 goto error_free_channels;
508 ret = iio_device_register(indio_dev);
510 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
511 goto error_disable_reg;
517 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
519 kfree(indio_dev->channels);
524 static void ad5360_remove(struct spi_device *spi)
526 struct iio_dev *indio_dev = spi_get_drvdata(spi);
527 struct ad5360_state *st = iio_priv(indio_dev);
529 iio_device_unregister(indio_dev);
531 kfree(indio_dev->channels);
533 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
536 static const struct spi_device_id ad5360_ids[] = {
537 { "ad5360", ID_AD5360 },
538 { "ad5361", ID_AD5361 },
539 { "ad5362", ID_AD5362 },
540 { "ad5363", ID_AD5363 },
541 { "ad5370", ID_AD5370 },
542 { "ad5371", ID_AD5371 },
543 { "ad5372", ID_AD5372 },
544 { "ad5373", ID_AD5373 },
547 MODULE_DEVICE_TABLE(spi, ad5360_ids);
549 static struct spi_driver ad5360_driver = {
553 .probe = ad5360_probe,
554 .remove = ad5360_remove,
555 .id_table = ad5360_ids,
557 module_spi_driver(ad5360_driver);
560 MODULE_DESCRIPTION("Analog Devices AD5360/61/62/63/70/71/72/73 DAC");
561 MODULE_LICENSE("GPL v2");