1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD7303 Digital to analog converters driver
5 * Copyright 2013 Analog Devices Inc.
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/kernel.h>
12 #include <linux/spi/spi.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/regulator/consumer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
20 #include <linux/platform_data/ad7303.h>
22 #define AD7303_CFG_EXTERNAL_VREF BIT(15)
23 #define AD7303_CFG_POWER_DOWN(ch) BIT(11 + (ch))
24 #define AD7303_CFG_ADDR_OFFSET 10
26 #define AD7303_CMD_UPDATE_DAC (0x3 << 8)
29 * struct ad7303_state - driver instance specific data
30 * @spi: the device for this driver instance
31 * @config: cached config register value
32 * @dac_cache: current DAC raw value (chip does not support readback)
33 * @vdd_reg: reference to VDD regulator
34 * @vref_reg: reference to VREF regulator
35 * @lock: protect writes and cache updates
36 * @data: spi transfer buffer
40 struct spi_device *spi;
44 struct regulator *vdd_reg;
45 struct regulator *vref_reg;
49 * DMA (thus cache coherency maintenance) requires the
50 * transfer buffers to live in their own cache lines.
52 __be16 data ____cacheline_aligned;
55 static int ad7303_write(struct ad7303_state *st, unsigned int chan,
58 st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC |
59 (chan << AD7303_CFG_ADDR_OFFSET) |
62 return spi_write(st->spi, &st->data, sizeof(st->data));
65 static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev,
66 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
68 struct ad7303_state *st = iio_priv(indio_dev);
70 return sprintf(buf, "%d\n", (bool)(st->config &
71 AD7303_CFG_POWER_DOWN(chan->channel)));
74 static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
75 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
78 struct ad7303_state *st = iio_priv(indio_dev);
82 ret = strtobool(buf, &pwr_down);
86 mutex_lock(&st->lock);
89 st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
91 st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel);
93 /* There is no noop cmd which allows us to only update the powerdown
94 * mode, so just write one of the DAC channels again */
95 ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
97 mutex_unlock(&st->lock);
101 static int ad7303_get_vref(struct ad7303_state *st,
102 struct iio_chan_spec const *chan)
106 if (st->config & AD7303_CFG_EXTERNAL_VREF)
107 return regulator_get_voltage(st->vref_reg);
109 ret = regulator_get_voltage(st->vdd_reg);
115 static int ad7303_read_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan, int *val, int *val2, long info)
118 struct ad7303_state *st = iio_priv(indio_dev);
122 case IIO_CHAN_INFO_RAW:
123 mutex_lock(&st->lock);
124 *val = st->dac_cache[chan->channel];
125 mutex_unlock(&st->lock);
127 case IIO_CHAN_INFO_SCALE:
128 vref_uv = ad7303_get_vref(st, chan);
132 *val = 2 * vref_uv / 1000;
133 *val2 = chan->scan_type.realbits;
135 return IIO_VAL_FRACTIONAL_LOG2;
142 static int ad7303_write_raw(struct iio_dev *indio_dev,
143 struct iio_chan_spec const *chan, int val, int val2, long mask)
145 struct ad7303_state *st = iio_priv(indio_dev);
149 case IIO_CHAN_INFO_RAW:
150 if (val >= (1 << chan->scan_type.realbits) || val < 0)
153 mutex_lock(&st->lock);
154 ret = ad7303_write(st, chan->address, val);
156 st->dac_cache[chan->channel] = val;
157 mutex_unlock(&st->lock);
166 static const struct iio_info ad7303_info = {
167 .read_raw = ad7303_read_raw,
168 .write_raw = ad7303_write_raw,
171 static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
174 .read = ad7303_read_dac_powerdown,
175 .write = ad7303_write_dac_powerdown,
176 .shared = IIO_SEPARATE,
181 #define AD7303_CHANNEL(chan) { \
182 .type = IIO_VOLTAGE, \
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
195 .ext_info = ad7303_ext_info, \
198 static const struct iio_chan_spec ad7303_channels[] = {
203 static int ad7303_probe(struct spi_device *spi)
205 const struct spi_device_id *id = spi_get_device_id(spi);
206 struct iio_dev *indio_dev;
207 struct ad7303_state *st;
210 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
211 if (indio_dev == NULL)
214 st = iio_priv(indio_dev);
215 spi_set_drvdata(spi, indio_dev);
219 mutex_init(&st->lock);
221 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
222 if (IS_ERR(st->vdd_reg))
223 return PTR_ERR(st->vdd_reg);
225 ret = regulator_enable(st->vdd_reg);
229 st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF");
230 if (IS_ERR(st->vref_reg)) {
231 ret = PTR_ERR(st->vref_reg);
233 goto err_disable_vdd_reg;
238 ret = regulator_enable(st->vref_reg);
240 goto err_disable_vdd_reg;
242 st->config |= AD7303_CFG_EXTERNAL_VREF;
245 indio_dev->name = id->name;
246 indio_dev->info = &ad7303_info;
247 indio_dev->modes = INDIO_DIRECT_MODE;
248 indio_dev->channels = ad7303_channels;
249 indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
251 ret = iio_device_register(indio_dev);
253 goto err_disable_vref_reg;
257 err_disable_vref_reg:
259 regulator_disable(st->vref_reg);
261 regulator_disable(st->vdd_reg);
265 static int ad7303_remove(struct spi_device *spi)
267 struct iio_dev *indio_dev = spi_get_drvdata(spi);
268 struct ad7303_state *st = iio_priv(indio_dev);
270 iio_device_unregister(indio_dev);
273 regulator_disable(st->vref_reg);
274 regulator_disable(st->vdd_reg);
279 static const struct of_device_id ad7303_spi_of_match[] = {
280 { .compatible = "adi,ad7303", },
283 MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
285 static const struct spi_device_id ad7303_spi_ids[] = {
289 MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
291 static struct spi_driver ad7303_driver = {
294 .of_match_table = ad7303_spi_of_match,
296 .probe = ad7303_probe,
297 .remove = ad7303_remove,
298 .id_table = ad7303_spi_ids,
300 module_spi_driver(ad7303_driver);
302 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
303 MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
304 MODULE_LICENSE("GPL v2");