1 // SPDX-License-Identifier: GPL-2.0-only
3 * Analog devices AD5764, AD5764R, AD5744, AD5744R quad-channel
4 * 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 AD5764_REG_SF_NOP 0x0
22 #define AD5764_REG_SF_CONFIG 0x1
23 #define AD5764_REG_SF_CLEAR 0x4
24 #define AD5764_REG_SF_LOAD 0x5
25 #define AD5764_REG_DATA(x) ((2 << 3) | (x))
26 #define AD5764_REG_COARSE_GAIN(x) ((3 << 3) | (x))
27 #define AD5764_REG_FINE_GAIN(x) ((4 << 3) | (x))
28 #define AD5764_REG_OFFSET(x) ((5 << 3) | (x))
30 #define AD5764_NUM_CHANNELS 4
33 * struct ad5764_chip_info - chip specific information
34 * @int_vref: Value of the internal reference voltage in uV - 0 if external
35 * reference voltage is used
36 * @channels: channel specification
38 struct ad5764_chip_info
{
39 unsigned long int_vref
;
40 const struct iio_chan_spec
*channels
;
44 * struct ad5764_state - driver instance specific data
46 * @chip_info: chip info
47 * @vref_reg: vref supply regulators
48 * @lock: lock to protect the data buffer during SPI ops
49 * @data: spi transfer buffers
53 struct spi_device
*spi
;
54 const struct ad5764_chip_info
*chip_info
;
55 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(&st
->lock
);
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(&st
->lock
);
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(&st
->lock
);
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(&st
->lock
);
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
->name
= spi_get_device_id(spi
)->name
;
293 indio_dev
->info
= &ad5764_info
;
294 indio_dev
->modes
= INDIO_DIRECT_MODE
;
295 indio_dev
->num_channels
= AD5764_NUM_CHANNELS
;
296 indio_dev
->channels
= st
->chip_info
->channels
;
298 mutex_init(&st
->lock
);
300 if (st
->chip_info
->int_vref
== 0) {
301 st
->vref_reg
[0].supply
= "vrefAB";
302 st
->vref_reg
[1].supply
= "vrefCD";
304 ret
= devm_regulator_bulk_get(&st
->spi
->dev
,
305 ARRAY_SIZE(st
->vref_reg
), st
->vref_reg
);
307 dev_err(&spi
->dev
, "Failed to request vref regulators: %d\n",
312 ret
= regulator_bulk_enable(ARRAY_SIZE(st
->vref_reg
),
315 dev_err(&spi
->dev
, "Failed to enable vref regulators: %d\n",
321 ret
= iio_device_register(indio_dev
);
323 dev_err(&spi
->dev
, "Failed to register iio device: %d\n", ret
);
324 goto error_disable_reg
;
330 if (st
->chip_info
->int_vref
== 0)
331 regulator_bulk_disable(ARRAY_SIZE(st
->vref_reg
), st
->vref_reg
);
335 static int ad5764_remove(struct spi_device
*spi
)
337 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
338 struct ad5764_state
*st
= iio_priv(indio_dev
);
340 iio_device_unregister(indio_dev
);
342 if (st
->chip_info
->int_vref
== 0)
343 regulator_bulk_disable(ARRAY_SIZE(st
->vref_reg
), st
->vref_reg
);
348 static const struct spi_device_id ad5764_ids
[] = {
349 { "ad5744", ID_AD5744
},
350 { "ad5744r", ID_AD5744R
},
351 { "ad5764", ID_AD5764
},
352 { "ad5764r", ID_AD5764R
},
355 MODULE_DEVICE_TABLE(spi
, ad5764_ids
);
357 static struct spi_driver ad5764_driver
= {
361 .probe
= ad5764_probe
,
362 .remove
= ad5764_remove
,
363 .id_table
= ad5764_ids
,
365 module_spi_driver(ad5764_driver
);
367 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
368 MODULE_DESCRIPTION("Analog Devices AD5744/AD5744R/AD5764/AD5764R DAC");
369 MODULE_LICENSE("GPL v2");