1 // SPDX-License-Identifier: GPL-2.0
3 * AD8366 and similar Gain Amplifiers
4 * This driver supports the following gain amplifiers:
5 * AD8366 Dual-Digital Variable Gain Amplifier (VGA)
6 * ADA4961 BiCMOS RF Digital Gain Amplifier (DGA)
7 * ADL5240 Digitally controlled variable gain amplifier (VGA)
8 * HMC792A 0.25 dB LSB GaAs MMIC 6-Bit Digital Attenuator
9 * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator
11 * Copyright 2012-2019 Analog Devices Inc.
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/spi/spi.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/err.h>
22 #include <linux/module.h>
23 #include <linux/bitrev.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
42 struct spi_device *spi;
43 struct regulator *reg;
44 struct mutex lock; /* protect sensor state */
45 struct gpio_desc *reset_gpio;
47 enum ad8366_type type;
48 struct ad8366_info *info;
50 * DMA (thus cache coherency maintenance) may require the
51 * transfer buffers to live in their own cache lines.
53 unsigned char data[2] __aligned(IIO_DMA_MINALIGN);
56 static struct ad8366_info ad8366_infos[] = {
79 static int ad8366_write(struct iio_dev *indio_dev,
80 unsigned char ch_a, unsigned char ch_b)
82 struct ad8366_state *st = iio_priv(indio_dev);
87 ch_a = bitrev8(ch_a & 0x3F);
88 ch_b = bitrev8(ch_b & 0x3F);
90 st->data[0] = ch_b >> 4;
91 st->data[1] = (ch_b << 4) | (ch_a >> 2);
94 st->data[0] = ch_a & 0x1F;
97 st->data[0] = (ch_a & 0x3F);
105 ret = spi_write(st->spi, st->data, indio_dev->num_channels);
107 dev_err(&indio_dev->dev, "write failed (%d)", ret);
112 static int ad8366_read_raw(struct iio_dev *indio_dev,
113 struct iio_chan_spec const *chan,
118 struct ad8366_state *st = iio_priv(indio_dev);
122 mutex_lock(&st->lock);
124 case IIO_CHAN_INFO_HARDWAREGAIN:
125 code = st->ch[chan->channel];
129 gain = code * 253 + 4500;
132 gain = 15000 - code * 1000;
135 gain = 20000 - 31500 + code * 500;
138 gain = -1 * code * 500;
141 gain = -1 * code * 250;
147 *val2 = (gain % 1000) * 1000;
149 ret = IIO_VAL_INT_PLUS_MICRO_DB;
154 mutex_unlock(&st->lock);
159 static int ad8366_write_raw(struct iio_dev *indio_dev,
160 struct iio_chan_spec const *chan,
165 struct ad8366_state *st = iio_priv(indio_dev);
166 struct ad8366_info *inf = st->info;
172 gain = (val * 1000) - (val2 / 1000);
174 gain = (val * 1000) + (val2 / 1000);
176 if (gain > inf->gain_max || gain < inf->gain_min)
181 code = (gain - 4500) / 253;
184 code = (15000 - gain) / 1000;
187 code = ((gain - 500 - 20000) / 500) & 0x3F;
190 code = (abs(gain) / 500) & 0x3F;
193 code = (abs(gain) / 250) & 0x7F;
197 mutex_lock(&st->lock);
199 case IIO_CHAN_INFO_HARDWAREGAIN:
200 st->ch[chan->channel] = code;
201 ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
206 mutex_unlock(&st->lock);
211 static int ad8366_write_raw_get_fmt(struct iio_dev *indio_dev,
212 struct iio_chan_spec const *chan,
216 case IIO_CHAN_INFO_HARDWAREGAIN:
217 return IIO_VAL_INT_PLUS_MICRO_DB;
223 static const struct iio_info ad8366_info = {
224 .read_raw = &ad8366_read_raw,
225 .write_raw = &ad8366_write_raw,
226 .write_raw_get_fmt = &ad8366_write_raw_get_fmt,
229 #define AD8366_CHAN(_channel) { \
230 .type = IIO_VOLTAGE, \
233 .channel = _channel, \
234 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
237 static const struct iio_chan_spec ad8366_channels[] = {
242 static const struct iio_chan_spec ada4961_channels[] = {
246 static int ad8366_probe(struct spi_device *spi)
248 struct iio_dev *indio_dev;
249 struct ad8366_state *st;
252 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
253 if (indio_dev == NULL)
256 st = iio_priv(indio_dev);
258 st->reg = devm_regulator_get(&spi->dev, "vcc");
259 if (!IS_ERR(st->reg)) {
260 ret = regulator_enable(st->reg);
265 spi_set_drvdata(spi, indio_dev);
266 mutex_init(&st->lock);
268 st->type = spi_get_device_id(spi)->driver_data;
272 indio_dev->channels = ad8366_channels;
273 indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
279 st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_HIGH);
280 if (IS_ERR(st->reset_gpio)) {
281 ret = PTR_ERR(st->reset_gpio);
282 goto error_disable_reg;
284 indio_dev->channels = ada4961_channels;
285 indio_dev->num_channels = ARRAY_SIZE(ada4961_channels);
288 dev_err(&spi->dev, "Invalid device ID\n");
290 goto error_disable_reg;
293 st->info = &ad8366_infos[st->type];
294 indio_dev->name = spi_get_device_id(spi)->name;
295 indio_dev->info = &ad8366_info;
296 indio_dev->modes = INDIO_DIRECT_MODE;
298 ret = ad8366_write(indio_dev, 0, 0);
300 goto error_disable_reg;
302 ret = iio_device_register(indio_dev);
304 goto error_disable_reg;
309 if (!IS_ERR(st->reg))
310 regulator_disable(st->reg);
315 static void ad8366_remove(struct spi_device *spi)
317 struct iio_dev *indio_dev = spi_get_drvdata(spi);
318 struct ad8366_state *st = iio_priv(indio_dev);
319 struct regulator *reg = st->reg;
321 iio_device_unregister(indio_dev);
324 regulator_disable(reg);
327 static const struct spi_device_id ad8366_id[] = {
328 {"ad8366", ID_AD8366},
329 {"ada4961", ID_ADA4961},
330 {"adl5240", ID_ADL5240},
331 {"hmc792a", ID_HMC792},
332 {"hmc1119", ID_HMC1119},
335 MODULE_DEVICE_TABLE(spi, ad8366_id);
337 static struct spi_driver ad8366_driver = {
339 .name = KBUILD_MODNAME,
341 .probe = ad8366_probe,
342 .remove = ad8366_remove,
343 .id_table = ad8366_id,
346 module_spi_driver(ad8366_driver);
349 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
350 MODULE_LICENSE("GPL v2");