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 * HMC1119 0.25 dB LSB, 7-Bit, Silicon Digital Attenuator
10 * Copyright 2012-2019 Analog Devices Inc.
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/spi/spi.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/err.h>
21 #include <linux/module.h>
22 #include <linux/bitrev.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
40 struct spi_device
*spi
;
41 struct regulator
*reg
;
42 struct mutex lock
; /* protect sensor state */
43 struct gpio_desc
*reset_gpio
;
45 enum ad8366_type type
;
46 struct ad8366_info
*info
;
48 * DMA (thus cache coherency maintenance) requires the
49 * transfer buffers to live in their own cache lines.
51 unsigned char data
[2] ____cacheline_aligned
;
54 static struct ad8366_info ad8366_infos
[] = {
73 static int ad8366_write(struct iio_dev
*indio_dev
,
74 unsigned char ch_a
, unsigned char ch_b
)
76 struct ad8366_state
*st
= iio_priv(indio_dev
);
81 ch_a
= bitrev8(ch_a
& 0x3F);
82 ch_b
= bitrev8(ch_b
& 0x3F);
84 st
->data
[0] = ch_b
>> 4;
85 st
->data
[1] = (ch_b
<< 4) | (ch_a
>> 2);
88 st
->data
[0] = ch_a
& 0x1F;
91 st
->data
[0] = (ch_a
& 0x3F);
98 ret
= spi_write(st
->spi
, st
->data
, indio_dev
->num_channels
);
100 dev_err(&indio_dev
->dev
, "write failed (%d)", ret
);
105 static int ad8366_read_raw(struct iio_dev
*indio_dev
,
106 struct iio_chan_spec
const *chan
,
111 struct ad8366_state
*st
= iio_priv(indio_dev
);
115 mutex_lock(&st
->lock
);
117 case IIO_CHAN_INFO_HARDWAREGAIN
:
118 code
= st
->ch
[chan
->channel
];
122 gain
= code
* 253 + 4500;
125 gain
= 15000 - code
* 1000;
128 gain
= 20000 - 31500 + code
* 500;
131 gain
= -1 * code
* 250;
137 *val2
= (gain
% 1000) * 1000;
139 ret
= IIO_VAL_INT_PLUS_MICRO_DB
;
144 mutex_unlock(&st
->lock
);
149 static int ad8366_write_raw(struct iio_dev
*indio_dev
,
150 struct iio_chan_spec
const *chan
,
155 struct ad8366_state
*st
= iio_priv(indio_dev
);
156 struct ad8366_info
*inf
= st
->info
;
162 gain
= (val
* 1000) - (val2
/ 1000);
164 gain
= (val
* 1000) + (val2
/ 1000);
166 if (gain
> inf
->gain_max
|| gain
< inf
->gain_min
)
171 code
= (gain
- 4500) / 253;
174 code
= (15000 - gain
) / 1000;
177 code
= ((gain
- 500 - 20000) / 500) & 0x3F;
180 code
= (abs(gain
) / 250) & 0x7F;
184 mutex_lock(&st
->lock
);
186 case IIO_CHAN_INFO_HARDWAREGAIN
:
187 st
->ch
[chan
->channel
] = code
;
188 ret
= ad8366_write(indio_dev
, st
->ch
[0], st
->ch
[1]);
193 mutex_unlock(&st
->lock
);
198 static int ad8366_write_raw_get_fmt(struct iio_dev
*indio_dev
,
199 struct iio_chan_spec
const *chan
,
203 case IIO_CHAN_INFO_HARDWAREGAIN
:
204 return IIO_VAL_INT_PLUS_MICRO_DB
;
210 static const struct iio_info ad8366_info
= {
211 .read_raw
= &ad8366_read_raw
,
212 .write_raw
= &ad8366_write_raw
,
213 .write_raw_get_fmt
= &ad8366_write_raw_get_fmt
,
216 #define AD8366_CHAN(_channel) { \
217 .type = IIO_VOLTAGE, \
220 .channel = _channel, \
221 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),\
224 static const struct iio_chan_spec ad8366_channels
[] = {
229 static const struct iio_chan_spec ada4961_channels
[] = {
233 static int ad8366_probe(struct spi_device
*spi
)
235 struct iio_dev
*indio_dev
;
236 struct ad8366_state
*st
;
239 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
240 if (indio_dev
== NULL
)
243 st
= iio_priv(indio_dev
);
245 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
246 if (!IS_ERR(st
->reg
)) {
247 ret
= regulator_enable(st
->reg
);
252 spi_set_drvdata(spi
, indio_dev
);
253 mutex_init(&st
->lock
);
255 st
->type
= spi_get_device_id(spi
)->driver_data
;
259 indio_dev
->channels
= ad8366_channels
;
260 indio_dev
->num_channels
= ARRAY_SIZE(ad8366_channels
);
265 st
->reset_gpio
= devm_gpiod_get_optional(&spi
->dev
, "reset", GPIOD_OUT_HIGH
);
266 if (IS_ERR(st
->reset_gpio
)) {
267 ret
= PTR_ERR(st
->reset_gpio
);
268 goto error_disable_reg
;
270 indio_dev
->channels
= ada4961_channels
;
271 indio_dev
->num_channels
= ARRAY_SIZE(ada4961_channels
);
274 dev_err(&spi
->dev
, "Invalid device ID\n");
276 goto error_disable_reg
;
279 st
->info
= &ad8366_infos
[st
->type
];
280 indio_dev
->name
= spi_get_device_id(spi
)->name
;
281 indio_dev
->info
= &ad8366_info
;
282 indio_dev
->modes
= INDIO_DIRECT_MODE
;
284 ret
= ad8366_write(indio_dev
, 0 , 0);
286 goto error_disable_reg
;
288 ret
= iio_device_register(indio_dev
);
290 goto error_disable_reg
;
295 if (!IS_ERR(st
->reg
))
296 regulator_disable(st
->reg
);
301 static int ad8366_remove(struct spi_device
*spi
)
303 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
304 struct ad8366_state
*st
= iio_priv(indio_dev
);
305 struct regulator
*reg
= st
->reg
;
307 iio_device_unregister(indio_dev
);
310 regulator_disable(reg
);
315 static const struct spi_device_id ad8366_id
[] = {
316 {"ad8366", ID_AD8366
},
317 {"ada4961", ID_ADA4961
},
318 {"adl5240", ID_ADL5240
},
319 {"hmc1119", ID_HMC1119
},
322 MODULE_DEVICE_TABLE(spi
, ad8366_id
);
324 static struct spi_driver ad8366_driver
= {
326 .name
= KBUILD_MODNAME
,
328 .probe
= ad8366_probe
,
329 .remove
= ad8366_remove
,
330 .id_table
= ad8366_id
,
333 module_spi_driver(ad8366_driver
);
335 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
336 MODULE_DESCRIPTION("Analog Devices AD8366 and similar Gain Amplifiers");
337 MODULE_LICENSE("GPL v2");