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/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #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 * @data: spi transfer buffer
37 struct spi_device *spi;
41 struct regulator *vdd_reg;
42 struct regulator *vref_reg;
45 * DMA (thus cache coherency maintenance) requires the
46 * transfer buffers to live in their own cache lines.
48 __be16 data ____cacheline_aligned;
51 static int ad7303_write(struct ad7303_state *st, unsigned int chan,
54 st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC |
55 (chan << AD7303_CFG_ADDR_OFFSET) |
58 return spi_write(st->spi, &st->data, sizeof(st->data));
61 static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev,
62 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
64 struct ad7303_state *st = iio_priv(indio_dev);
66 return sprintf(buf, "%d\n", (bool)(st->config &
67 AD7303_CFG_POWER_DOWN(chan->channel)));
70 static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
71 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
74 struct ad7303_state *st = iio_priv(indio_dev);
78 ret = strtobool(buf, &pwr_down);
82 mutex_lock(&indio_dev->mlock);
85 st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
87 st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel);
89 /* There is no noop cmd which allows us to only update the powerdown
90 * mode, so just write one of the DAC channels again */
91 ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
93 mutex_unlock(&indio_dev->mlock);
97 static int ad7303_get_vref(struct ad7303_state *st,
98 struct iio_chan_spec const *chan)
102 if (st->config & AD7303_CFG_EXTERNAL_VREF)
103 return regulator_get_voltage(st->vref_reg);
105 ret = regulator_get_voltage(st->vdd_reg);
111 static int ad7303_read_raw(struct iio_dev *indio_dev,
112 struct iio_chan_spec const *chan, int *val, int *val2, long info)
114 struct ad7303_state *st = iio_priv(indio_dev);
118 case IIO_CHAN_INFO_RAW:
119 *val = st->dac_cache[chan->channel];
121 case IIO_CHAN_INFO_SCALE:
122 vref_uv = ad7303_get_vref(st, chan);
126 *val = 2 * vref_uv / 1000;
127 *val2 = chan->scan_type.realbits;
129 return IIO_VAL_FRACTIONAL_LOG2;
136 static int ad7303_write_raw(struct iio_dev *indio_dev,
137 struct iio_chan_spec const *chan, int val, int val2, long mask)
139 struct ad7303_state *st = iio_priv(indio_dev);
143 case IIO_CHAN_INFO_RAW:
144 if (val >= (1 << chan->scan_type.realbits) || val < 0)
147 mutex_lock(&indio_dev->mlock);
148 ret = ad7303_write(st, chan->address, val);
150 st->dac_cache[chan->channel] = val;
151 mutex_unlock(&indio_dev->mlock);
160 static const struct iio_info ad7303_info = {
161 .read_raw = ad7303_read_raw,
162 .write_raw = ad7303_write_raw,
165 static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
168 .read = ad7303_read_dac_powerdown,
169 .write = ad7303_write_dac_powerdown,
170 .shared = IIO_SEPARATE,
175 #define AD7303_CHANNEL(chan) { \
176 .type = IIO_VOLTAGE, \
180 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
181 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
189 .ext_info = ad7303_ext_info, \
192 static const struct iio_chan_spec ad7303_channels[] = {
197 static int ad7303_probe(struct spi_device *spi)
199 const struct spi_device_id *id = spi_get_device_id(spi);
200 struct iio_dev *indio_dev;
201 struct ad7303_state *st;
205 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
206 if (indio_dev == NULL)
209 st = iio_priv(indio_dev);
210 spi_set_drvdata(spi, indio_dev);
214 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
215 if (IS_ERR(st->vdd_reg))
216 return PTR_ERR(st->vdd_reg);
218 ret = regulator_enable(st->vdd_reg);
222 if (spi->dev.of_node) {
223 ext_ref = of_property_read_bool(spi->dev.of_node,
226 struct ad7303_platform_data *pdata = spi->dev.platform_data;
227 if (pdata && pdata->use_external_ref)
234 st->vref_reg = devm_regulator_get(&spi->dev, "REF");
235 if (IS_ERR(st->vref_reg)) {
236 ret = PTR_ERR(st->vref_reg);
237 goto err_disable_vdd_reg;
240 ret = regulator_enable(st->vref_reg);
242 goto err_disable_vdd_reg;
244 st->config |= AD7303_CFG_EXTERNAL_VREF;
247 indio_dev->dev.parent = &spi->dev;
248 indio_dev->name = id->name;
249 indio_dev->info = &ad7303_info;
250 indio_dev->modes = INDIO_DIRECT_MODE;
251 indio_dev->channels = ad7303_channels;
252 indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
254 ret = iio_device_register(indio_dev);
256 goto err_disable_vref_reg;
260 err_disable_vref_reg:
262 regulator_disable(st->vref_reg);
264 regulator_disable(st->vdd_reg);
268 static int ad7303_remove(struct spi_device *spi)
270 struct iio_dev *indio_dev = spi_get_drvdata(spi);
271 struct ad7303_state *st = iio_priv(indio_dev);
273 iio_device_unregister(indio_dev);
276 regulator_disable(st->vref_reg);
277 regulator_disable(st->vdd_reg);
282 static const struct of_device_id ad7303_spi_of_match[] = {
283 { .compatible = "adi,ad7303", },
286 MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
288 static const struct spi_device_id ad7303_spi_ids[] = {
292 MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
294 static struct spi_driver ad7303_driver = {
297 .of_match_table = of_match_ptr(ad7303_spi_of_match),
299 .probe = ad7303_probe,
300 .remove = ad7303_remove,
301 .id_table = ad7303_spi_ids,
303 module_spi_driver(ad7303_driver);
305 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
306 MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
307 MODULE_LICENSE("GPL v2");