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)
9 * Copyright 2012-2019 Analog Devices Inc.
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/bitrev.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
38 struct spi_device *spi;
39 struct regulator *reg;
40 struct mutex lock; /* protect sensor state */
41 struct gpio_desc *reset_gpio;
43 enum ad8366_type type;
44 struct ad8366_info *info;
46 * DMA (thus cache coherency maintenance) requires the
47 * transfer buffers to live in their own cache lines.
49 unsigned char data[2] ____cacheline_aligned;
52 static struct ad8366_info ad8366_infos[] = {
67 static int ad8366_write(struct iio_dev *indio_dev,
68 unsigned char ch_a, unsigned char ch_b)
70 struct ad8366_state *st = iio_priv(indio_dev);
75 ch_a = bitrev8(ch_a & 0x3F);
76 ch_b = bitrev8(ch_b & 0x3F);
78 st->data[0] = ch_b >> 4;
79 st->data[1] = (ch_b << 4) | (ch_a >> 2);
82 st->data[0] = ch_a & 0x1F;
85 st->data[0] = (ch_a & 0x3F);
89 ret = spi_write(st->spi, st->data, indio_dev->num_channels);
91 dev_err(&indio_dev->dev, "write failed (%d)", ret);
96 static int ad8366_read_raw(struct iio_dev *indio_dev,
97 struct iio_chan_spec const *chan,
102 struct ad8366_state *st = iio_priv(indio_dev);
106 mutex_lock(&st->lock);
108 case IIO_CHAN_INFO_HARDWAREGAIN:
109 code = st->ch[chan->channel];
113 gain = code * 253 + 4500;
116 gain = 15000 - code * 1000;
119 gain = 20000 - 31500 + code * 500;
125 *val2 = (gain % 1000) * 1000;
127 ret = IIO_VAL_INT_PLUS_MICRO_DB;
132 mutex_unlock(&st->lock);
137 static int ad8366_write_raw(struct iio_dev *indio_dev,
138 struct iio_chan_spec const *chan,
143 struct ad8366_state *st = iio_priv(indio_dev);
144 struct ad8366_info *inf = st->info;
150 gain = (val * 1000) - (val2 / 1000);
152 gain = (val * 1000) + (val2 / 1000);
154 if (gain > inf->gain_max || gain < inf->gain_min)
159 code = (gain - 4500) / 253;
162 code = (15000 - gain) / 1000;
165 code = ((gain - 500 - 20000) / 500) & 0x3F;
169 mutex_lock(&st->lock);
171 case IIO_CHAN_INFO_HARDWAREGAIN:
172 st->ch[chan->channel] = code;
173 ret = ad8366_write(indio_dev, st->ch[0], st->ch[1]);
178 mutex_unlock(&st->lock);
183 static const struct iio_info ad8366_info = {
184 .read_raw = &ad8366_read_raw,
185 .write_raw = &ad8366_write_raw,
188 #define AD8366_CHAN(_channel) { \
189 .type = IIO_VOLTAGE, \
192 .channel = _channel, \
193 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
196 static const struct iio_chan_spec ad8366_channels[] = {
201 static const struct iio_chan_spec ada4961_channels[] = {
205 static int ad8366_probe(struct spi_device *spi)
207 struct iio_dev *indio_dev;
208 struct ad8366_state *st;
211 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
212 if (indio_dev == NULL)
215 st = iio_priv(indio_dev);
217 st->reg = devm_regulator_get(&spi->dev, "vcc");
218 if (!IS_ERR(st->reg)) {
219 ret = regulator_enable(st->reg);
224 spi_set_drvdata(spi, indio_dev);
225 mutex_init(&st->lock);
227 st->type = spi_get_device_id(spi)->driver_data;
231 indio_dev->channels = ad8366_channels;
232 indio_dev->num_channels = ARRAY_SIZE(ad8366_channels);
236 st->reset_gpio = devm_gpiod_get(&spi->dev, "reset",
238 indio_dev->channels = ada4961_channels;
239 indio_dev->num_channels = ARRAY_SIZE(ada4961_channels);
242 dev_err(&spi->dev, "Invalid device ID\n");
244 goto error_disable_reg;
247 st->info = &ad8366_infos[st->type];
248 indio_dev->dev.parent = &spi->dev;
249 indio_dev->name = spi_get_device_id(spi)->name;
250 indio_dev->info = &ad8366_info;
251 indio_dev->modes = INDIO_DIRECT_MODE;
253 ret = ad8366_write(indio_dev, 0 , 0);
255 goto error_disable_reg;
257 ret = iio_device_register(indio_dev);
259 goto error_disable_reg;
264 if (!IS_ERR(st->reg))
265 regulator_disable(st->reg);
270 static int ad8366_remove(struct spi_device *spi)
272 struct iio_dev *indio_dev = spi_get_drvdata(spi);
273 struct ad8366_state *st = iio_priv(indio_dev);
274 struct regulator *reg = st->reg;
276 iio_device_unregister(indio_dev);
279 regulator_disable(reg);
284 static const struct spi_device_id ad8366_id[] = {
285 {"ad8366", ID_AD8366},
286 {"ada4961", ID_ADA4961},
287 {"adl5240", ID_ADL5240},
290 MODULE_DEVICE_TABLE(spi, ad8366_id);
292 static struct spi_driver ad8366_driver = {
294 .name = KBUILD_MODNAME,
296 .probe = ad8366_probe,
297 .remove = ad8366_remove,
298 .id_table = ad8366_id,
301 module_spi_driver(ad8366_driver);
303 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
304 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
305 MODULE_LICENSE("GPL v2");